function validateEmail(email) {
	// a very simple email validation checking.
	// you can add more complex email checking if it helps
	if(email.length <= 0) {
		return true;
	}
	var splitted = email.match("^(.+)@(.+)$");
	if(splitted == null) return false;
	if(splitted[1] != null ) {
		var regexp_user=/^\"?[\w-_\.]*\"?$/;
		if(splitted[1].match(regexp_user) == null) return false;
	}
	if(splitted[2] != null) {
		var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
		if(splitted[2].match(regexp_domain) == null) {
			var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
			if(splitted[2].match(regexp_ip) == null) return false;
		}// if
		return true;
	}
	return false;
}


function LuhnCheck(str) {
        var result = true;

        var sum = 0;
        var mul = 1;
        var strLen = str.length;

        for (i = 0; i < strLen; i++) {
                var digit = str.substring(strLen-i-1,strLen-i);
                var tproduct = parseInt(digit ,10)*mul;
                if (tproduct >= 10)
                        sum += (tproduct % 10) + 1;
                else
                        sum += tproduct;
                if (mul == 1)
                        mul++;
                else
                        mul--;
        }
        if ((sum % 10) != 0)
                result = false;

        return result;
}

function checkMatch(pw1Element,pw1Description,pw2Element,pw2Description,msg) {
       p1e = document.getElementById(pw1Element);
       p2e = document.getElementById(pw2Element);
       p1d = document.getElementById(pw1Description);
       p2d = document.getElementById(pw2Description);
       err = "";

       clearFailLabel(p2d,pw2Element);
       if(p1e.value != p2e.value) {
               validateFailLabel(pw2Element,pw2Description)
               err = msg + "\n";
       }
       return err;
}

function clearFailLabel(myDesc,elemValue) {
	if(myDesc && myDesc.old_color && myDesc.old_color_setBy == elemValue){
		if(myDesc.old_color == '-NONE-')  {
			myDesc.style.color="";
		} else {
			myDesc.style.color=myDesc.old_color;
		}
		myDesc.old_color_setBy = null;
	}
}

function trim(obj) {
	 a = obj.value.replace(/^\s+/, '');
	obj.value = a.replace(/\s+$/, '');
}

function validate(elemValue,dispText,valType,required,minlen,maxlen,elemDescription) {

	myVal = document.getElementById(elemValue);
	myDesc = document.getElementById(elemDescription);

	err = ""

	trim(myVal);


	clearFailLabel(myDesc,elemValue);

	if(required && (myVal== null || myVal.value.length == 0 )) {
		err += dispText + " is Required\n";
	} else {
		if(minlen && minlen > 0 && (!myVal || myVal.value.length < minlen)) {
			err += dispText + ": Minimum length is " + minlen + "\n";
		}
		if(maxlen && maxlen > 0 && (!myVal || myVal.value.length > maxlen)) {
			err += dispText + ": Maximum length is " + maxlen + "\n";
		}
		switch(valType) {
			case "alpha":
				var charpos = myVal.value.search("[^A-Za-z]");
                        	if(myVal.value.length > 0 &&  charpos >= 0) {
					err += dispText + ": Only alphabetic characters allowed\n";
				}
				break;

			case "alphanum":
				var charpos = myVal.value.search("[^A-Za-z0-9]");
				if(myVal.value.length > 0 &&  charpos >= 0) {
					err += dispText + ": Only alphanumeric characters allowed\n";
				}
				break;
			case "num":
				var charpos = myVal.value.search("[^0-9]");
				if(myVal.value.length > 0 &&  charpos >= 0) {
					err += dispText + ": Only digits allowed\n";
				}
				break;
			case "terms":
				if(!myVal.checked)
					err += "You must agree to the terms and conditions\n";
				break;
			case "email":
				if(myVal.value.length && !validateEmail(myVal.value))
					err += "Please enter a valid email address\n";
				break;
			case "cc":
				if(myVal.value.length && !LuhnCheck(myVal.value))
					err += "Please enter a valid Credit Card\n";
				break;
			break;
		}
	}
	if(err.length > 0 ) {
		validateFailLabel(elemValue,myDesc);
	}

	return err;

}

function validateFailLabel(elemName,labelObj) {
	if(typeof(labelObj) == "string") {
		labelId = document.getElementById(labelObj);
	} else {
		labelId = labelObj;
	}
	if(labelId && !labelId.old_color_setBy) {
		if(labelId.style.color)
			labelId.old_color = labelId.style.color;
		else
			labelId.old_color = "-NONE-";
		labelId.old_color_setBy = elemName;
		labelId.style.color="#ff0000";
	}
}

