/**
 *	Validate the form and other utilites functions.
 *
 *	by Tsuen "Chris" Siu
 */
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	checkNotEmpty
//	Return true if the string is not empty, otherwise return false.
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkNotEmpty( strValue ) {
/*alert("strValue: " + strValue );*/
   if(strValue && (strValue.length > 0) ){
     return true;
   }
   return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 	replace the string with the string 
//	var textAreaStr = document.getElementById("optionalTextarea1").value;
//	document.getElementById("optionalTextarea1").value = replaceStr(textAreaStr, "<br/>")
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function replaceStr(orgStr, replaceWith) {
	var returnStr;
	returnStr = escape( orgStr ) //encode textarea string's carriage returns
	for(i=0; i<returnStr.length; i++){
		//loop through string, replacing carriage return encoding with HTML break tag
		if(returnStr.indexOf("%0D%0A") > -1){
			//Windows encodes returns as \r\n hex
			returnStr = returnStr.replace("%0D%0A",replaceWith)
		} else if(returnStr.indexOf("%0A") > -1){
			//Unix encodes returns as \n hex
			returnStr = returnStr.replace("%0A",replaceWith)
		} else if(returnStr.indexOf("%0D") > -1){
			//Macintosh encodes returns as \r hex
			returnStr = returnStr.replace("%0D",replaceWith)
		}
	}
	returnStr=unescape(returnStr) //unescape all other encoded characters
	return returnStr;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Check is the date is valid
//	Return true if the date is valid, otherwise return false;
// 	e.g. 02/29/2008 or 05/24/1970
//
//	REF: http://www.breakingpar.com/bkp/home.nsf/0/75A5C758B9DF576087256AFB00140789
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkValidDate(dateStr) {
    // dateStr must be of format month day year with either slashes
    // or dashes separating the parts. Some minor changes would have
    // to be made to use day month year or another format.
    // This function returns True if the date is valid.
    var slash1 = dateStr.indexOf("/");
    if (slash1 == -1) { slash1 = dateStr.indexOf("-"); }
    // if no slashes or dashes, invalid date
    if (slash1 == -1) { return false; }
    var dateMonth = dateStr.substring(0, slash1)
    var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
    var slash2 = dateMonthAndYear.indexOf("/");
    if (slash2 == -1) { slash2 = dateMonthAndYear.indexOf("-"); }
    // if not a second slash or dash, invalid date
    if (slash2 == -1) { return false; }
    var dateDay = dateMonthAndYear.substring(0, slash2);
    var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
    if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ) { return false; }
    // if any non-digits in the month, invalid date
    for (var x=0; x < dateMonth.length; x++) {
        var digit = dateMonth.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text month to a number
    var numMonth = 0;
    for (var x=0; x < dateMonth.length; x++) {
        digit = dateMonth.substring(x, x+1);
        numMonth *= 10;
        numMonth += parseInt(digit);
    }
    if ((numMonth <= 0) || (numMonth > 12)) { return false; }
    // if any non-digits in the day, invalid date
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text day to a number
    var numDay = 0;
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        numDay *= 10;
        numDay += parseInt(digit);
    }
    if ((numDay <= 0) || (numDay > 31)) { return false; }
    // February can't be greater than 29 (leap year calculation comes later)
    if ((numMonth == 2) && (numDay > 29)) { return false; }
    // check for months with only 30 days
    if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) {
        if (numDay > 30) { return false; }
    }
    // if any non-digits in the year, invalid date
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text year to a number
    var numYear = 0;
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        numYear *= 10;
        numYear += parseInt(digit);
    }
    // Year must be a 2-digit year or a 4-digit year
    if ( (dateYear.length != 2) && (dateYear.length != 4) ) { return false; }
    // if 2-digit year, use 50 as a pivot date
    if ( (numYear < 50) && (dateYear.length == 2) ) { numYear += 2000; }
    if ( (numYear < 100) && (dateYear.length == 2) ) { numYear += 1900; }
    if ((numYear <= 0) || (numYear > 9999)) { return false; }
    // check for leap year if the month and day is Feb 29
    if ((numMonth == 2) && (numDay == 29)) {
        var div4 = numYear % 4;
        var div100 = numYear % 100;
        var div400 = numYear % 400;
        // if not divisible by 4, then not a leap year so Feb 29 is invalid
        if (div4 != 0) { return false; }
        // at this point, year is divisible by 4. So if year is divisible by
        // 100 and not 400, then it's not a leap year so Feb 29 is invalid
        if ((div100 == 0) && (div400 != 0)) { return false; }
    }
    // date is valid
    return true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Check is the email address is valid
//	Return true if the email address is valid, otherwise return false;
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkValidEmail(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}
	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	 if (str.indexOf(" ")!=-1){
	    return false
	 }
	 return true					
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Check is the file has a valid file extension
//	Return true if the file has the file extension otherwise return false.
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkValidFileExt(str, ext) {
	if (!checkNotEmpty(str) || !checkNotEmpty(ext) ) {
		return false;
	} else {
		var rx=new RegExp("[^\.]\."+ext+"\s*$", "i");
		return rx.test(str);
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Calculate the age using day, month and year.
//	Return the age in number (integer)
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function calAge(bday,bmo,byr) {
	var byr;
	var age;
	var now = new Date();
	tday=now.getDate();
	tmo=(now.getMonth()+1); /* for some reason the month is 4 for may */
	tyr=(now.getFullYear()); 
	{
		if((tmo > bmo)||(tmo==bmo & tday>=bday)) {
			age=byr;
		} else {
			age=byr+1;
		}
	}
	return (tyr-age);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	removeCarriageReturn
//	
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function removeCarriageReturn() { 
	var textAreaStr = document.getElementById("optionalTextarea1").value; document.getElementById("optionalTextarea1").value = replaceStr(textAreaStr, "<br/>") 
} 

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	validateForm
//	return the validate object.  
//	Preperties: 
//		- error_msg
//		- isValid
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function validateForm(frm) {
	/*
//alert('validate form: ' + frm );
	var objValidate = new Object();
	var alertMessage = "";
    var isAlert = false;
	var MAX_DESC_CHAR = 10000; 
	//name
	if( !checkNotEmpty( frm.txtName.value ) ) { 
		alertMessage += "Please enter your name. <br />"; 
		isAlert = true; 
	}
	//email
	if(  !checkNotEmpty( frm.txtFrom.value ) ) {   
		alertMessage += "Please enter your e-mail. <br />";
		isAlert = true;
	} else {
		//is it valid
		if ( !checkValidEmail( frm.txtFrom.value ) ) {
			alertMessage += "Please enter your valid email address. <br />";
			isAlert = true;
		}
	}
	//question or comment	
	if( !checkNotEmpty( frm.txtBody.value ) ) { 
		alertMessage += "Please enter your question or comment. <br />"; 
		isAlert = true; 
	} else {
		var num_charaters = frm.optionalTextarea1.value.length;; 
		if(num_charaters > MAX_DESC_CHAR) { 
			alertMessage += "Oops! Your question or comment is too long. The total number of characters including space in your feedback is " + num_charaters + ". The maximum we are able to accept is " + MAX_DESC_CHAR + ". <br />"; 
			isAlert = true; 
		 } 
	}
	objValidate.error_msg = alertMessage;
	objValidate.isValid = !isAlert;
	return objValidate;
	*/
	return true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	validateFeedBackFrom
//	return the validate object.  
//	Preperties: 
//		- error_msg
//		- isValid
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function validateFeedBackFrom(frm) {
	var objValidate = new Object();
	var alertMessage = "";
    var isAlert = false;
	var MAX_DESC_CHAR = 10000; 

	//type of feedback
	if (!checkNotEmpty( frm.optionalTextarea2.value ) ) {
		alertMessage += "Please select the type of feedback. <br />"; 
		isAlert = true; 
	}
	//name
	if( !checkNotEmpty( frm.optionalText1.value ) ) { 
		alertMessage += "Please enter your name. <br />"; 
		isAlert = true; 
	}
	//email
	if(  !checkNotEmpty( frm.optionalText2.value ) ) {   
		alertMessage += "Please enter your e-mail. <br />";
		isAlert = true;
	} else {
		//is it valid
		if ( !checkValidEmail( frm.optionalText2.value ) ) {
			alertMessage += "Please enter your valid email address. <br />";
			isAlert = true;
		}
	}
	//school	
	if(  !checkNotEmpty( frm.optionalText3.value ) ) {   
		alertMessage += "Please enter your school. <br />";
		isAlert = true;
	}
	//Graduation Year
	if(  !checkNotEmpty( frm.optionalText4.value ) ) {   
		alertMessage += "Please enter your graduation year. <br />";
		isAlert = true;
	}
	//Major:
	if(  !checkNotEmpty( frm.optionalText5.value ) ) {   
		alertMessage += "Please enter your major. <br />";
		isAlert = true;
	}
	//the upload photo..
	if( ( checkNotEmpty( frm.uploadEntryFile.value ) && !checkValidFileExt( frm.uploadEntryFile.value, "jpg" ) ) ||
		( checkNotEmpty( frm.uploadEntryFile2.value ) && !checkValidFileExt( frm.uploadEntryFile2.value, "jpg" ) ) ||
		( checkNotEmpty( frm.uploadEntryFile3.value ) && !checkValidFileExt( frm.uploadEntryFile3.value, "jpg" ) ) ||
		( checkNotEmpty( frm.uploadEntryFile4.value ) && !checkValidFileExt( frm.uploadEntryFile4.value, "jpg" ) ) ||
		( checkNotEmpty( frm.uploadEntryFile5.value ) && !checkValidFileExt( frm.uploadEntryFile5.value, "jpg" ) ) ) {   
		alertMessage += "Please upload only jpg files. <br />";
		isAlert = true;
	}
	//question or comment	
	if( !checkNotEmpty( frm.optionalTextarea1.value ) ) { 
		alertMessage += "Please enter your question or comment. <br />"; 
		isAlert = true; 
	} else {
		var num_charaters = frm.optionalTextarea1.value.length;; 
		if(num_charaters > MAX_DESC_CHAR) { 
			alertMessage += "Oops! Your question or comment is too long. The total number of characters including space in your feedback is " + num_charaters + ". The maximum we are able to accept is " + MAX_DESC_CHAR + ". <br />"; 
			isAlert = true; 
		 } 
	}
	objValidate.error_msg = alertMessage;
	objValidate.isValid = !isAlert;
	return objValidate;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	Validate the invite your frineds from.
//	Return true if it is valid, otherwise return false
//	return the validate object.  
//	Preperties: 
//		- error_msg
//		- isValid
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function validateInivteYourFriendsFrom(frm) {
	var objValidate = new Object();
	var alertMessage = "";
    var isAlert = false;
	var MAX_DESC_CHAR = 10000; 
	//name
	if(  !checkNotEmpty( frm.sendername.value ) ) {   
		alertMessage += "Please enter your name. <br />";
		isAlert = true;
	}
	//email
	if(  !checkNotEmpty( frm.youremail.value ) ) {   
		alertMessage += "Please enter your e-mail. <br />";
		isAlert = true;
	} else {
		//is it valid
		if ( !checkValidEmail( frm.youremail.value ) ) {
			alertMessage += "Please enter your valid email address. <br />";
			isAlert = true;
		}
	}
	//friend 1 
	if(  !checkNotEmpty( frm.email1.value ) ) {   
		alertMessage += "Please enter your friend's e-mail. <br />";
		isAlert = true;
	} else {
		//is it valid
		if ( !checkValidEmail( frm.email1.value ) ) {   
			alertMessage += "Please enter your friend's valid email address. <br />";
			isAlert = true;
		}
	}
	//friend 2
	if (!isAlert) {
		if(  checkNotEmpty( frm.email2.value ) &&
			 !checkValidEmail( frm.email2.value )	) {   
			alertMessage += "Please enter your friend's valid email address. <br />";
			isAlert = true;		
		}
	}
	//friend 3
	if (!isAlert) {
		if(  checkNotEmpty( frm.email3.value ) &&
			 !checkValidEmail( frm.email3.value )	) {   
			alertMessage += "Please enter your friend's valid email address. <br />";
			isAlert = true;		
		}
	}
	objValidate.error_msg = alertMessage;
	objValidate.isValid = !isAlert;
	return objValidate;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	getShareLink
//	get the share link from the
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function getShareLink(frm) {
	var sShareLink = "";
	if(  checkNotEmpty( frm.share_link_1.value ) ) {   	
		sShareLink = sShareLink + "<link>" + frm.share_link_1.value + "</link>";
	}
	if(  checkNotEmpty( frm.share_link_2.value ) ) {   	
		sShareLink = sShareLink + "<link>" + frm.share_link_2.value + "</link>";
	}
	if(  checkNotEmpty( frm.share_link_3.value ) ) {   	
		sShareLink = sShareLink + "<link>" + frm.share_link_3.value + "</link>";
	}
	if(  checkNotEmpty( frm.share_link_4.value ) ) {   	
		sShareLink = sShareLink + "<link>" + frm.share_link_4.value + "</link>";
	}
	if(  checkNotEmpty( frm.share_link_5.value ) ) {   	
		sShareLink = sShareLink + "<link>" + frm.share_link_5.value + "</link>";
	}
	return sShareLink;
}
