// JavaScript Document

var title, fname,lname,addr1,city,state,zip, dayarea, daypre, daysuf, email = new String();

var result;

//initialise this variable to true by default. no error!
var errorreg = true;
var errorString = "";


//validate for numeric digits or anydigits contain  in the bag of string
function validateNumeric(s,bag){
	var errorflag = true;
	for(var i=0; i < s.length; i++){
		var c = s.charAt(i);
		if(bag.indexOf(c) == -1)
			errorflag = false;
		
	}
	//alert("validate: " + s + " : " + errorflag);
	return errorflag;
}

//Validating email fields function
function validateFields(){

   errorString ="";
   title = document.sweepsForm.TITLE.value;
   fname = document.sweepsForm.FIRST_NAME.value;
   lname = document.sweepsForm.LAST_NAME.value;
   addr1 = document.sweepsForm.ADDRESS.value;
   city = document.sweepsForm.CITY.value;
   state = document.sweepsForm.STATE.options[document.sweepsForm.STATE.selectedIndex].value;
   zip = document.sweepsForm.ZIP.value;
   dayarea = document.sweepsForm.PHONE_AREA_CODE.value;
   daypre = document.sweepsForm.PHONE_EXCHANGE.value;
   daysuf = document.sweepsForm.PHONE_ENDING.value;
   email = document.sweepsForm.EMAIL.value;
	
   //validate title
   if(title.length < 1){
      errorreg = false;
      errorString += "Please enter your Title to continue.\n"; 
   }
   
   //validate fname
   if(fname.length < 1 || fname.length > 20){
	   errorreg = false;
       errorString += "Please enter your First Name to continue.\n"; 
   }
   
   //validate lname
   if(lname.length < 1 || lname.length > 30){
      errorreg = false;
      errorString += "Please enter your Last Name to continue.\n"; 
   }
   
   //validate address
   if(addr1.length < 1 || addr1.length > 40){
      errorreg = false;
      errorString += "Please enter your Address to continue.\n"; 
   }
   
   //validate city
   if(city.length < 1 || city.length > 35){
      errorreg = false;
      errorString += "Please enter your City to continue.\n"; 
   }
   
   //validate state
   if(state.length < 1){
      errorreg = false;
      errorString += "Please enter your State to continue.\n"; 
   }
   
   //validate zip code
   if(zip.length < 1 || zip.length > 5 || zip.length != 5){
      errorreg = false;
      errorString += "Please enter last 5 digits zip code to continue.\n"; 
   } else if(zip.length == 5 ){
   	  var bagString = "0123456789";
   	  erroract = validateNumeric(zip,bagString);
     	//alert("acct no : " + erroract);
      if(!erroract){
   		 errorreg = false;
         errorString += "Please enter last 5 digits of your zip code to continue.\n"; 
      }
   } 
 
   //validate day time phone area
   if(dayarea.length < 1 || dayarea.length > 3 || dayarea.length != 3){
      errorreg = false;
      errorString += "Please enter 3 digit Phone Area Code to continue.\n"; 
   } else if(dayarea.length == 3 ){
   	  var bagString = "0123456789";
   	  erroract = validateNumeric(dayarea,bagString);
     	//alert("acct no : " + erroract);
      if(!erroract){
   		 errorreg = false;
         errorString += "Please enter 3 digit Phone Area Code to continue.\n"; 
      }
   } 
   
   //validate day time phone prefix  
   if(daypre.length < 1 || daypre.length > 3 || daypre.length != 3){
      errorreg = false;
      errorString += "Please enter 3 digit Phone Prefix to continue.\n"; 
   } else if(daypre.length == 3 ){
   	  var bagString = "0123456789";
   	  erroract = validateNumeric(daypre,bagString);
     	//alert("acct no : " + erroract);
      if(!erroract){
   		 errorreg = false;
         errorString += "Please enter 3 digit Phone Prefix to continue.\n"; 
      }
   } 
	
   //validate day time phone surfix   
   if(daysuf.length < 1 || daysuf.length > 4 || daysuf.length != 4){
      errorreg = false;
      errorString += "Please enter 4 digit Phone Suffix to continue.\n"; 
   } else if(daysuf.length == 4 ){
   	  var bagString = "0123456789";
   	  erroract = validateNumeric(daysuf,bagString);
     	//alert("acct no : " + erroract);
      if(!erroract){
   		 errorreg = false;
         errorString += "Please enter 4 digit Phone Suffix to continue.\n"; 
      }
   } 
   
   //validate email
   var regexp = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
   if(email.length < 1 || email.length > 50 || email.search(regexp) == -1){
       errorreg = false;
       errorString += "Please enter your E-mail Address to continue.\n"; 
   }
	

   if (!document.sweepsForm.ACCEPT_FLAG.checked)
   {
       errorreg = false;
       errorString += "Make sure you have read and accept the rules for this Contest.\n"; 
   }

//finally return the flag.
return errorString;
}

// for email form
function submitForm(){

	//validate fields first
	result = validateFields();
	if(result != ""){
		if(errorString.length > 0){
			alert(errorString);
			return false;
		}
	} else {
		sendLinkEvent('scrippshgtv','2008 GMC Lifestyle Sweepstakes Entry Form Submitted','o');
	    return true;
	}
}

/* END VALIDATION */

function popWin(url,winName,attributes) {
	if (attributes.indexOf("directories") == -1) { attributes += ",directories=0"; }
	if (attributes.indexOf("location") == -1) {	attributes += ",location=0"; }
	if (attributes.indexOf("menubar") == -1) { attributes += ",menubar=0"; }
	if (attributes.indexOf("resizable") == -1) { attributes += ",resizable=0"; }
	if (attributes.indexOf("scrollbars") == -1) { attributes += ",scrollbars=1"; }
	if (attributes.indexOf("status") == -1) { attributes += ",status=0"; }
	if (attributes.indexOf("toolbar") == -1) { attributes += ",toolbar=0"; }
	if (attributes.indexOf("top") == -1) { attributes += ",top=80";	}
	if (attributes.indexOf("left") == -1) { attributes += ",left=50"; }
	
	var newWindow = window.open(url,winName,attributes);
	if (window.focus && newWindow) { newWindow.focus() }
}