// list of form fields to check and with what function

function checkClass(p_inputObj,p_action,p_params){
	this.input = p_inputObj;
	this.action = p_action;
	this.params = p_params;
	return this;
}

function submitForm(p_formObj,p_fieldRules){
	if(checkWholeForm(p_formObj,p_fieldRules)){	
		document.qualify.action = "https://qawebw1.roxio.com/Qflix/submit.jhtml";
		//alert(document.qualify.MD_INTEREST.value);
		document.qualify.submit();
	}else{
		return false;
	}
}




function checkWholeForm(p_formObj,p_fieldRules) {
    var errMsg = "";
    
    for(nn=0;nn < p_fieldRules.length;nn++){
    	switch (this.checkTheseFields[nn].action){
    		case "isEmpty":
    			errMsg += isEmpty(p_fieldRules[nn].input.value,p_fieldRules[nn].params);
    			break;
    		case "checkEmail":
    			errMsg += checkEmail(p_fieldRules[nn].input.value,p_fieldRules[nn].params);
    			break;
    		case "checkPhone":
    			errMsg += checkPhone(p_fieldRules[nn].input.value,p_fieldRules[nn].params);
    			break;
    		case "checkSelect":
    			errMsg += checkSelect(p_fieldRules[nn].input.selectedIndex,p_fieldRules[nn].params);
    			break;
    		case "confirmEmail":
    			//param has the matching source
    			errMsg += matchInput(p_fieldRules[nn].input,p_fieldRules[nn].params)
    			//alert("Match results=" + p_fieldRules[nn].input.value);
    			break;
			case "checkTextArea":
				//alert("Textarea check=" + p_fieldRules[nn].input.value);
    			errMsg += isEmpty(p_fieldRules[nn].input.value,p_fieldRules[nn].params);
    			break;
			case "checkLimit":
				errMsg += checkCharLimit(p_fieldRules[nn].input.value,p_fieldRules[nn].params);
				break;
    		default:
    			alert("didn't find a valid action!" + p_fieldRules[nn].input.name);
    			break;
    	}
    }
    if (errMsg != "") {
       alert(errMsg);
       return false;
    }
    return true;
   }
// match


function securityCheck(p_str){
	var validCharList = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '!.?-,+*$@();:"
	var tmpStr = ""
    for(nn=0; nn < p_str.length; nn++){
       if (validCharList.indexOf(p_str.charAt(nn)) != -1){
          tmpStr += p_str.charAt(nn)
	   }
	}
	return tmpStr;
}

function selDropObj(p_text,p_val){
	this.text = p_text;
	this.val = p_val;
	return this;
}
	
	
function drawSelect(p_name,p_data,p_selectedIndex){
// a utility function to draw out the select box
var tmpHTML = '<select name="' + p_name + '" > \n'
	for(nn=0;nn<p_data.length;nn++){
		tmpHTML += '<option value="' + p_data[nn].val + '"'
		tmpHTML += (nn == p_selectedIndex) ? ' SELECTED>' : '>'
		tmpHTML += p_data[nn].text + ' \n';
	}
	tmpHTML += '</select> \n'
	//alert(tmpHTML);
	document.write(tmpHTML);
};

function matchInput(p_source,p_destination){
	// this function sets the text from one select to a hidden value
	//alert("called match!" + p_source.type);
	var tmpReturn = false;  // pessimistic

	switch (p_source.type.toLowerCase()){
		case "text":
			tmpReturn = (p_source.value == p_destination.value)
			break;
		//case "select":
			//tmpReturn = (p_source.selectedIndex == p_destination..selectedIndex)
			//break;
			
		default:
		//notning
	}
	if(!tmpReturn){
		return "Your Email Address must match your Email Address Confirmation. \n"
	}else{
		return ""
	}
		
	//p_destination.value = p_source.options[p_source.selectedIndex].value
}


// email

function checkEmail (strng, msg) {
var error="";
if (strng == "") {
   error = "You didn't enter an email address.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\n";
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng, msg) {
	var error = "";
	if (strng == "") {
   		error = "Please this enter a phone number.\n";
	}

	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters." + strng + " \n";
  
    }
    if (!(stripped.length >= 10)) {
	error = "The phone number: " + stripped + " is the wrong length. Make sure you included an area code.\n";
    } 
	return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng, msg) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 6) || (strng.length > 8)) {
       error = "The password is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\n";
    } 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng, msg) {
var error = "";
if (strng == "") {
   error = "You didn't enter a username.\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "The username is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
    error = "The username contains illegal characters.\n";
    } 
return error;
}       


// non-empty textbox

function isEmpty(strng, msg) {
var error = "";
  if (strng.length == 0) {
     error = msg + " is a required field. Please fill in the " + msg + " field.\n"
  }
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;
}

// valid selector from dropdown list

function checkSelect(choice, msg) {
var error = "";
    if (choice == 0) {
    error = "Please choose an option from the " + msg + " drop-down list.\n";
    }    
return error;
}    

function checkCharLimit(strng , params){
	var error = ""
	if(strng.length > params[0]){
		error = "Please reduce the size of your " + params[1]
	}
return error;
}    

	
