// Name:  formFunctions.js
// Author:  RLM
// Description:  Used to validate the submit form
// itsr#0967 -- SKS -- 06/02/2009 -- added formDispType as parameter to 
//                     displayOneCntry(doc, country, formDispType).
//                     Added logic for shortForm
// itsr#0965 -- SKS -- 07/23/2009 -- added eduBgd,profExp,question fields to store in cookies.
// itsr#1058/release2 -- KA -- 11/23/2009 -- added 'email2', 'secondGrad' , 'birthDay' , 'birthMon' , 'birthYr' , 
// 											'advisor' , 'phoneCel', 'structure', 'desc1' , 'comment' ,  'campus', 'eduBgd', 'profExp', 'preferredCampus',
//											'startQtr', 'yearsAttend', 'question' fields to the cookie
//
function isField(vform, vname) {
   var fldExist = false;
   for (i=0; i<vform.length; i++) 
   {
      if (vform[i].name == vname)
      {
      	return(true);
      } 
   }
   return(fldExist);
}

function setFormCookies(vform) {
		var cookievalue = "";
		var cookievalueMultiple = "";

	for (i=0; i<vform.length; i++) {
		fieldName = vform[i].name;
		
		if (vform[i].name == 'firstName'  ||
            vform[i].name == 'lastName'  ||
            vform[i].name == 'address1'  ||
            vform[i].name == 'address2'  ||
            vform[i].name == 'city'  ||
            vform[i].name == 'state'  ||
            vform[i].name == 'country'  ||
            vform[i].name == 'province'  ||
            vform[i].name == 'zipCode'  ||
            vform[i].name == 'phoneArea'  ||
            vform[i].name == 'phoneTrans'  ||
            vform[i].name == 'phoneHome'  ||
            vform[i].name == 'email'  ||
            vform[i].name == 'gradYear' ||
            // added for ITSR 1058 _ release 2: added 'email2', 'secondGrad' , 'birthDay' , 'birthMon' , 'birthYr' , 
            // 'advisor' , 'phoneCel', 'structure', 'desc1' , 'comment' ,  'campus', 'eduBgd', 'profExp', 'preferredCampus',
            // 'startQtr', 'yearsAttend', 'question' fields to the cookie
            vform[i].name == 'email2'  ||
            vform[i].name == 'secondGrad' ||
            vform[i].name == 'birthDay' ||
            vform[i].name == 'birthMon' || 
            vform[i].name == 'birthYr' || 
            vform[i].name == 'advisor' ||  
            vform[i].name == 'phoneCel'	||
            vform[i].name == 'structure'||
            vform[i].name == 'desc1' ||
            vform[i].name == 'comment' ||
            vform[i].name == 'campus' ||
            vform[i].name == 'eduBgd' ||
            vform[i].name == 'profExp' ||
            vform[i].name == 'preferredCampus' ||
            vform[i].name == 'startQtr' ||
            vform[i].name == 'yearsAttend' ||
            vform[i].name == 'question') 	{
            setCookieValue(vform[i].name, vform[i].value);
    	} else if (vform[i].type == "radio") {
        	for (j=0; j<vform[fieldName].length; j++)
			{
				if (vform[fieldName][j].checked){
						setCookieValue(vform[i].name, vform[fieldName][j].value);
					}
			}
    	} else if (vform[i].type == "checkbox"){
     		var newCookie = "";
           	for (j=0; j<vform[fieldName].length; j++)
			{	
				if (vform[fieldName][j].checked){
					newCookie = vform[fieldName][j].value +',';
					cookievalue =  cookievalue + newCookie;
					}
		}
		setCookieValue(vform[i].name, cookievalue);
    	}else if (vform[i].type == "select-multiple"){
    		var multiple = "";
           	for (j=0; j<vform[fieldName].length; j++)
			{	
				if (vform[fieldName][j].selected){
					multiple = vform[fieldName][j].value +',';
					cookievalueMultiple =  cookievalueMultiple + multiple;
					}
			}
		setCookieValue(vform[i].name, cookievalueMultiple);
		}
 	}
    return(true)
}

function isNum(chkVal) {
   if(chkVal == "") {
        return false;
     }
     
     for(i=0; i < chkVal.length; i++) {
        if(chkVal.charAt(i) < "0") {
           return false
          }
          if(chkVal.charAt(i) > "9") {
             return false
          }
   }
   return true
}

function isEmailAddr(email) {
   var result = false;
   var theStr = new String(email);
   if(-1 == theStr.indexOf('(') &&  -1 == theStr.indexOf(')')) {
	   var index = theStr.indexOf("@");
	   if (index > 0) {
	      var pindex = theStr.indexOf(".",index);
	       if ((pindex > index+1) && (theStr.length > pindex+1))
	         result = true;
	   }
   }
   return result;
}

function trim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = /  /g;
   while (temp.match(obj)) { temp = temp.replace(obj, " "); }
   return temp;
}

function limitText(limitField, limitNum) {
   if (limitField.value.length > limitNum) {
      limitField.value = limitField.value.substring(0, limitNum);
   }
}
 
function validatePhoneCel(phoneFld){
   var phone = phoneFld.value;
   if (phone.length > 0 && phone !=' ' && phone !='  ' && phone !='   ' && phone != '    '){  
      for (i = 0; i < phone.length; i++){
         if(phone.charAt(i) < "0" || phone.charAt(i) > "9") {
               alert("Please enter digits only for cell phone#");
               phoneFld.focus();  
               return false;
            }        
      }
   }
   return true;
}

function validateGradYear(gradFld){
   var year = trim(gradFld.value);
   if (year.length > 0 && year != ' '){ 
      if (year.length < 4){
           alert("Please enter 4 digits for year");
            gradFld.focus();  
            return false;
        }   
        for (i = 0; i < 4; i++){
            if(year.charAt(i) < "0" || year.charAt(i) > "9") {
               alert("Please enter digits only for year");
               gradFld.focus();  
               return false;
             } 
        }
     }     
     return true;
}

function displayOneCntry(doc, country, formDispType){
   if(country.value.substring(0,3) == 'USA'){ 
   		if (formDispType === ' ') {
   		    doc.foreign1.style.display='none';
       		doc.foreign2.style.display='none';
       		doc.usa1.style.display='inline';
       		doc.usa2.style.display='inline';
   		}
       doc.postal.style.display='none';
       doc.intPhn1.style.display='none';
       doc.intPhn2.style.display='none';
       doc.zip.style.display='inline';
       doc.usaPhn1.style.display='inline';
       doc.usaPhn2.style.display='inline';
    } else { 
       	if (formDispType === ' ') {
   		    doc.foreign1.style.display='inline';
       		doc.foreign2.style.display='inline';
       		doc.usa1.style.display='none';
       		doc.usa2.style.display='none';
   		}
       doc.postal.style.display='inline';
       doc.intPhn1.style.display='inline';
       doc.intPhn2.style.display='inline';
       doc.zip.style.display='none';
       doc.usaPhn1.style.display='none';
       doc.usaPhn2.style.display='none';
    }
}

function displayContacted(doc, contacted){
   if(contacted.value == 'Yes'){ 
      doc.advisor1.style.display='inline';
      doc.advisor2.style.display='inline';
   } else { 
      doc.advisor1.style.display='none';
      doc.advisor2.style.display='none';
   }
}


  
