// JavaScript Document
function isReady_error(the_field)	// translate the field name to error messages
{
	switch(the_field)
	{
		case 'quant':
			return '<strong>Quantity field</strong> is empty.';
			break;
		case 'zipc':
			return '<strong>Zip code field</strong> is empty.';
			break;
		case 'zipc_format':
			return '<strong>Zip code field</strong> needs 5 digits.';
			break;
		case 'billzip':
			return '<strong>Zip code field</strong> is empty.';
			break;
		case 'billzip_format':
			return '<strong>Zip code field</strong> needs 5 digits.';
			break;
		case 'rgb1':
			return '<strong>Check color code field #1</strong>; Did you forget to enter your color code for 2 color print jobs? <br/>If you do not know what it is, please click on the "?" sign to learn more or if you do not know what to enter, please enter "not sure" in the color code field, thanks.';
		case 'rgb2':
			return '<strong>Check color code field #2</strong>; Did you forget to enter your color code for 2 color print jobs? <br/>If you do not know what it is, please click on the "?" sign to learn more or if you do not know what to enter, please enter "not sure" in the color code field, thanks.';
			break;
		case 'emailadd':
			return '<strong>Email address field</strong> is empty.';
			break;
		case 'emailadd_format':
			return '<strong>Email address format</strong> is wrong. Valid format example: craig@email.com';
			break;
		case 'emailadd_order':
			return '<strong>Email address field</strong> is empty.';
			break;
		case 'emailadd_order_format':
			return '<strong>Email address format</strong> is wrong. Valid format example: craig@email.com';
			break;
		case 'username':
			return '<strong>Full name field</strong> is empty.';
			break;
		case 'username_format':
			return '<strong>Full name format</strong> is wrong. Only characters are allowed for name field.';
			break;
		case 'username_order':
			return '<strong>Full name field</strong> is empty.';
			break;
		case 'username_order_format':
			return '<strong>Full name format</strong> is wrong. Only characters are allowed for name field.';
			break;
		case 'cardname':
			return '<strong>Cardholder name field</strong> is empty.';
			break;
		case 'cardname_format':
			return '<strong>Cardholder name format</strong> is wrong. Only characters are allowed for name field.';
			break;
		case 'qname':
			return '<strong>Your Name (Full Name) field</strong> is empty.';
			break;
		case 'qname_format':
			return '<strong>Your Name (Full Name) field format</strong> is wrong. Only characters are allowed for name field.';
			break;
		case 'phnum':
			return '<strong>Phone number field</strong> is empty.';
			break;
		case 'phnum_format':
			return '<strong>Phone number format</strong> is wrong. Valid format example: 870-852-2569';
			break;
		case 'phnum_order':
			return '<strong>Phone number field</strong> is empty.';
			break;
		case 'phnum_order_format':
			return '<strong>Phone number format</strong> is wrong. Valid format example: 870-852-2569';
			break;
		case 'phone':
			return '<strong>Phone number field</strong> is empty.';
			break;
		case 'phone_format':
			return '<strong>Phone number format</strong> is wrong. Valid format example: 870-852-2569';
			break;
		case 'cardtype':
			return '<strong>Credit card type: </strong>please pick a type.';
			break;
		case 'echeckname':
			return '<strong>Check Owner field </strong>is empty.';
			break;
		case 'echeckname_format':
			return '<strong>Check Owner format </strong> is wrong. Only characters are allowed for name field.';
			break;
		case 'eroutenum':
			return '<strong>Routing Number field </strong>is empty.';
			break;
		case 'eroutenum_format':
			return '<strong>Routing Number format </strong>is wrong. Routing Number have to be 9 digits. No characters are allowed';
			break;
		case 'eaccountnum':
			return '<strong>Account Number field </strong>is empty.';
			break;
		case 'eaccountnum_format':
			return '<strong>Account Number format </strong>is wrong. No character is allowed';
			break;
		case 'echecknum':
			return '<strong>Check number field </strong>is empty.';
			break;
		case 'echecknum_format':
			return '<strong>Check number format </strong>is wrong. No character is allowed';
			break;
		case 'echecktype':
			return '<strong>Check Type field </strong>is empty.';
			break;
		case 'echeckstate':
			return '<strong>Drivers License or Company State: </strong>please pick a state.';
			break;
		case 'docnumber':
			return '<strong>Drivers License or Federal Tax ID field </strong>is empty.';
			break;
		case 'cardnum':
			return '<strong>Credit card number </strong>is empty.';
			break;
		case 'cardnum_format':
			return '<strong>Credit card number </strong>format is wrong.';
			break;
		case 'cardcode':
			return '<strong>Security Code </strong>is empty.';
			break;
		case 'cardcode_format':
			return '<strong>Security Code </strong>format is wrong. Only 3 to 4 digits is allowed.';
			break;
		case 'cemonth':
			return '<strong>Expiration Date: </strong>please pick a month.';
			break;
		case 'ceyear':
			return '<strong>Expiration Date: </strong>please pick a year.';
			break;
		case 'billstate':
			return '<strong>State/ Region: </strong>please pick a state.';
			break;
		case 'shipstate':
			return '<strong>State/ Region: </strong>please pick a state for the shipping info.';
			break;
		case 'billadd':
			return '<strong>Billing address 1 </strong>is empty.';
			break;
		case 'billcity':
			return '<strong>Billing city </strong>is empty.';
			break;
		case 'billamt':
			return '<strong>Dollar amount </strong>is empty.';
			break;
		case 'billamt_format':
			return '<strong>Dollar amount </strong>format is wrong.';
			break;
		case 'billamt_amount':
			return '<strong>Dollar amount </strong>need to be at least $40.';
			break;
		case 'shipattn':
			return '<strong>Ship attention </strong>is empty.';
			break;
		case 'shipadd1':
			return '<strong>Ship address </strong>is empty.';
			break;
		case 'shipcity':
			return '<strong>Ship city </strong>is empty.';
			break;
		case 'shipzip':
			return '<strong>Shipping zip code </strong>is empty.';
			break;
		case 'iagree':
			return '<strong>Terms & Conditions: </strong>please check the check box to agree with our terms & conditions.';
			break;
		case 'quotenum':
			return '<strong>Quote number </strong>is empty.';
			break;
		case 'sender_name':
			return '<strong>Sender Name </strong>is empty.';
			break;
		case 'sender_name_format':
			return '<strong>Sender Name </strong>format is wrong.';
			break;
		case 'sender_emailadd':
			return '<strong>Sender Email Address </strong>is empty.';
			break;
		case 'sender_emailadd_format':
			return '<strong>Sender Email Address </strong>format is wrong.';
			break;
		case 'recipient_name':
			return '<strong>Recipient Name </strong>is empty.';
			break;
		case 'recipient_name_format':
			return '<strong>Recipient Name </strong>format is wrong.';
			break;
		case 'recipient_emailadd':
			return '<strong>Recipient Email Address </strong>is empty.';
			break;
		case 'recipient_emailadd_format':
			return '<strong>Recipient Email Address </strong>format is wrong.';
			break;
		case 'InquiryNumber':	//InquiryNumber = job number in ups tracking
			return '<strong>Job Number Field </strong>is empty.';
			break;
		case 'custom_size':	//custom quote page
			return '<strong>Custom Size Field </strong>is empty.';
			break;
		case 'custom_paper':	//custom quote page
			return '<strong>Custom Paper Field </strong>is empty.';
			break;
		case 'custom_color':	//custom quote page
			return '<strong>Custom Color Field </strong>is empty.';
			break;
		case 'front_rgb1':	//custom quote page
			return '<strong>Front Color Code 1 Field </strong>is empty.';
			break;
		case 'front_rgb2':	//custom quote page
			return '<strong>Front Color Code 2 Field </strong>is empty.';
			break;
		case 'back_rgb1':	//custom quote page
			return '<strong>Back Color Code 1 Field </strong>is empty.';
			break;
		case 'back_rgb2':	//custom quote page
			return '<strong>Back Color Code 2 Field </strong>is empty.';
			break;
		case 'custom_fold':	//custom quote page
			return '<strong>Custom Fold Field </strong>is empty.';
			break;
		case 'num_page':	//custom quote page
			return '<strong>Number of Page Field </strong>is empty.';
			break;
		case 'num_page_format':	//custom quote page
			return '<strong>Number of Page Field </strong>format is wrong. Only numbers are allowed.';
			break;
		case 'company':	//referral program
			return '<strong>Company / Organization field </strong>is empty.';
			break;
		case 'comname':	//ftp
			return '<strong>Company / Organization field </strong>is empty.';
			break;
		case 'address':	//referral program
			return '<strong>Address field </strong>is empty.';
			break;
		case 'shipcomname':	//ftp
			return '<strong>Shipping Info: Company / Organization field </strong>is empty.';
			break;
		case 'clienttype':	//ftp
			return '<strong>Are you a New Customer or Return Customer?';
			break;
		case 'clienttype':	//ftp
			return '<strong>Are you a New Customer or Return Customer?';
			break;
		case 'jobtitle':	//referral program
			return '<strong>Job Title Field</strong> is empty.';
			break;
		case 'shipname':	//referral program
			return 'Shipping Info: <strong>Full Name Field</strong> is empty.';
			break;
		case 'jobtitle':	//referral program
			return '<strong>Job Title Field</strong> is empty.';
			break;
		case 'city':	//referral program
			return '<strong>City field </strong>is empty.';
			break;
		case 'state':	//referral program
			return '<strong>State/ Region: </strong>please pick a state.';
			break;
		case 'the_industry':	//sample signup
			return 'Could you tell us <strong>what your industry is?</strong>';
			break;
		case 'other_industry':	//sample signup
			return 'Could you tell us <strong>what your industry is?</strong>';
			break;
		case 'the_role':	//sample signup
			return 'Could you tell us <strong>what your role is?</strong>';
			break;
		case 'other_role':	//sample signup
			return 'Could you tell us <strong>what your role is?</strong>';
			break;
		case 'next_print_job':	//sample signup
			return 'Could you tell us <strong>what your next print job is?</strong>';
			break;
		case 'outside_us':	//sample signup
			return 'Live outside US? Great! <strong>Could you tell us the state/ region?</strong>';
			break;
		case 'platform':	//file transfer
			return 'Could you tell us <strong>the platform which the file is for?</strong>';
			break;
		case 'application':	//file transfer
			return 'Could you tell us <strong>the file type of the file you are going to upload?</strong>';
			break;
		case 'application_other':	//file transfer
			return 'Could you specify <strong>the file type of the file you are going to upload?</strong>';
			break;
		case 'attn':	//file transfer
			return 'Could you tell us <strong>which customer service rep. would you like to contact?</strong>';
			break;
		case 'file1':	//file transfer
			return 'In order to upload a file <strong>we need at least one file to be selected.</strong>';
			break;
		case 'varnish':	//for postcard only
			return 'Please let us know what kind of <strong>finish </strong> do you want for your project.';
			break;
	}
}

var undef;
inputclasserror = 'inputError';
inputclassnormal = 'quote_text_field';
inputhighlight = true;	//highlight the text field border

function highlight(el, clazz) {	//highlight the text field border
    if (el!=undef && inputhighlight) {
        el.className = clazz;
    }
}

function isReady()	//argument are arrays of the field's name // validate all fields see if they are ready to submit, called by validateit() from x_general.js from all quote pages
{
	var items = isReady.arguments.length;	// to get array as arguments
	var err_message = new Array();
	var j = 0;
	
	reg_email = new RegExp("^(([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}){0,1}$");
	reg_name = new RegExp("^(([A-Za-z])+[.]?( )*)*$");	// only character and space allow
	reg_phone = new RegExp("^([1-9])([0-9]){2}-([0-9]){3}-([0-9]){4}$");
	reg_security_code = new RegExp("^([0-9]){4}$|^([0-9]){3}$");	//only 3 -4 digits is allowed
	reg_dollar_amount = new RegExp("^([0-9]+[.])([0-9]+)$|^([0-9]+)$");	//eg. 5.00000 OR 5
	reg_num = new RegExp("^([0-9]+)$"); //eg. 1234
	
	for (i = 0;i < items;i++)
	{
		field_name = isReady.arguments[i];
		theChoiceValue = document.Forminfo[field_name].value;
		theChoiceDefaultValue = document.Forminfo[field_name].defaultValue;
		theChoiceValue = strTrim(theChoiceValue);	//remove leading & tailing spaces
		
		if(field_name == "billamt" || field_name == "num_page")	//remove leading zero & char
		{
			theChoiceValue = remove_zero(theChoiceValue);	//remove leading zero
			theChoiceValue = remove_char(theChoiceValue);	//remove characters
		}
		
		if(field_name == "cardnum" || field_name == "cardcode" || field_name == "zipc" || field_name == "billzip" || field_name == "shipzip" || field_name == "eroutenum" || field_name == "eaccountnum")	//InquiryNumber = job number in ups tracking
		{
			theChoiceValue = remove_char(theChoiceValue);	//remove characters
		}

		if(field_name == "username" || field_name == "qname" || field_name == "cardname" || field_name == "shipattn" || field_name == "sender_name" || field_name == "recipient_name" || field_name == "echeckname")	//remove number for names
		{
			theChoiceValue = remove_num(theChoiceValue);
			theChoiceValue = remove_multi_space(theChoiceValue);
		}
		
		if(field_name == "billamt")	//remove leading $ sign
		{
			theChoiceValue = remove_dollar_sign(theChoiceValue);
		}

		if(field_name != "file1")	//can't modify file upload field
		{
			document.Forminfo[field_name].value = theChoiceValue;	//reset the text fields
		}
		//the first "if" below check all feilds if they are empty, also some special field for special input
		if((!theChoiceValue || theChoiceValue == theChoiceDefaultValue) && field_name != "iagree")	//iagree is a check box
		{
			err_message[j] = isReady_error(field_name);
			if(field_name != "file1")	//don't highlight file field
			{
				highlight(document.Forminfo.elements[field_name], inputclasserror);
			}
			j++;
		}
		else if((field_name == "zipc" || field_name == "billzip" || field_name == "shipzip") && theChoiceValue.length < 5)	//check if zip code is < 5 digits
		{
			err_message[j] = isReady_error(field_name+"_format");
			highlight(document.Forminfo.elements[field_name], inputclasserror);
			j++;	
		}
		else if((field_name == "eroutenum") && theChoiceValue.length < 9)	//check if routing number is < 9 digits
		{
			err_message[j] = isReady_error(field_name+"_format");
			highlight(document.Forminfo.elements[field_name], inputclasserror);
			j++;	
		}
		else if((field_name == "emailadd" || field_name == "emailadd_order" || field_name == "sender_emailadd" || field_name == "recipient_emailadd") && !reg_email.test(theChoiceValue))	//check email
		{
			err_message[j] = isReady_error(field_name+"_format");
			highlight(document.Forminfo.elements[field_name], inputclasserror);
			j++;	
		}
		else if((field_name == "username" || field_name == "username_order" || field_name == "qname" || field_name == "cardname" || field_name == "shipattn" || field_name == "sender_name" || field_name == "recipient_name" || field_name == "echeckname") && !reg_name.test(theChoiceValue))	//check name fields
		{
			err_message[j] = isReady_error(field_name+"_format");
			highlight(document.Forminfo.elements[field_name], inputclasserror);
			j++;	
		}
		/*else if((field_name == "phnum" || field_name == "phnum_order" || field_name == "phone") && !reg_phone.test(theChoiceValue))	//check phone number fields
		{
			err_message[j] = isReady_error(field_name+"_format");
			highlight(document.Forminfo.elements[field_name], inputclasserror);
			j++;	
		}*/
		else if((field_name == "num_page" || field_name == "eaccountnum" || field_name == "echecknum") && !reg_num.test(theChoiceValue))
		{
			err_message[j] = isReady_error(field_name+"_format");
			highlight(document.Forminfo.elements[field_name], inputclasserror);
			j++;
		}
		else if(field_name == "cardnum" && !checkCC(theChoiceValue))
		{
			err_message[j] = isReady_error(field_name+"_format");
			highlight(document.Forminfo.elements[field_name], inputclasserror);
			j++;
		}
		else if(field_name == "cardcode" && !reg_security_code.test(theChoiceValue))
		{
			err_message[j] = isReady_error(field_name+"_format");
			highlight(document.Forminfo.elements[field_name], inputclasserror);
			j++;
		}
		else if(field_name == "billamt" && !reg_dollar_amount.test(theChoiceValue))
		{
			err_message[j] = isReady_error(field_name+"_format");
			highlight(document.Forminfo.elements[field_name], inputclasserror);
			j++;
		}
		else if(field_name == "billamt" && parseInt(theChoiceValue) < 40 )
		{
			err_message[j] = isReady_error(field_name+"_amount");
			highlight(document.Forminfo.elements[field_name], inputclasserror);
			j++;
		}
		else if(field_name == "iagree" && !document.Forminfo[field_name].checked)
		{
			err_message[j] = isReady_error(field_name);
			highlight(document.Forminfo.elements[field_name], inputclasserror);
			j++;
		}
		else
		{
			if(field_name != "file1")	//don't need to unhighlight file field
			{
				highlight(document.Forminfo.elements[field_name], inputclassnormal);	//un-highlight the corrected field
			}
		}
	}
	return displayInnerHtml(err_message, HEADER_MSG_error, FOOTER_MSG_error, innererror_error);
}

function strTrim(str) {	//trim leading and tailing space
    return str.replace(/^\s+/,'').replace(/\s+$/,'');
}

function remove_zero(str)	//remove leading zero
{
	return 	str.replace(/^([0])+/,'')
}

function remove_num(str)	//remove numbers
{
	return 	str.replace(/([0-9])+/,'')
}

function remove_dollar_sign(str)	//remove numbers
{
	return 	str.replace(/(\$)+/,'')
}

function remove_char(str)	//remove characters
{
	return 	str.replace(/([A-Za-z])+/,'')
}

function remove_multi_space(str)	// remove double white space to one space
{
	return 	str.replace(/\s+/,' ')
}

// ------ variables for error messages ------//
HEADER_MSG_error = 'Sorry, but an error has been made:';	//header message
FOOTER_MSG_error = 'Please correct the error listed above and re-submit.';	//header message
errorsdiv = 'errorsDiv';	//div name on html
errorsdiv2 = 'errorsDiv2';	//div name on html
innererror_error = 'innerError';	//css name

function displayInnerHtml(messages, head_message, foot_message, inner_css) {	//display validation error messages on the page
    if ( messages!=null && messages.length>0 ) 
	{
        var str = '';
    	if (strTrim(head_message).length > 0) 
		{
            str += head_message;
        }
        str += '<ul>';
        for (var i=0; i<messages.length; i++) 
		{
            str += '<li>'+messages[i]+'</li>';
        }
        str += '</ul>';
    	if (strTrim(foot_message).length > 0) 
		{
            str += foot_message;
        }
        document.getElementById(errorsdiv).innerHTML = str;
        document.getElementById(errorsdiv).className = inner_css;
        document.getElementById(errorsdiv).style.display = 'block';
		
		if(document.getElementById(errorsdiv2))
		{
        	document.getElementById(errorsdiv2).innerHTML = str;
        	document.getElementById(errorsdiv2).className = inner_css;
        	document.getElementById(errorsdiv2).style.display = 'block';
		}
        return false;
    } 
	else 
	{
        document.getElementById(errorsdiv).innerHTML = '';
        document.getElementById(errorsdiv).className = '';
        document.getElementById(errorsdiv).style.display = 'none';
		
		if(document.getElementById(errorsdiv2))
		{
        	document.getElementById(errorsdiv2).innerHTML = '';
        	document.getElementById(errorsdiv2).className = '';
        	document.getElementById(errorsdiv2).style.display = 'none';
		}

        return true;	//if everything ok, we pop up a new window
    }
}

function submit_forms(submit_to, submit_from, special_task)	//may change this to submit_forms(this_form, submit_to, where_to_submit) - for file upload & some other non-quote page
{
	var should_submit = validateit();
	if (should_submit)
	{
		
		if (document.Forminfo.submit_type)	//if submit_type hidden field exist, do the following
		{
			document.Forminfo.submit_type.value = submit_from;	//when clicking a submit button, check which button is hitted
		}

		if(special_task != null && special_task == 'show_progress')	//for file transfer
		{
			ShowProgress();
		}
		
		document.Forminfo.action = submit_to;	//submit_to could be xyz.asp
		document.Forminfo.target = "_self";	//added for ups tracking in sub-modal
		
		return true;
	}
	else
	{
		return false;	
	}
}

function checkCC(s) {	//check credit card #

  var i, n, c, r, t;

  // First, reverse the string and remove any non-numeric characters.

  r = "";
  for (i = 0; i < s.length; i++) {
    c = parseInt(s.charAt(i), 10);
    if (c >= 0 && c <= 9)
      r = c + r;
  }

  // Check for a bad string.

  if (r.length <= 1)
    return false;

  // Now run through each single digit to create a new string. Even digits
  // are multiplied by two, odd digits are left alone.

  t = "";
  for (i = 0; i < r.length; i++) {
    c = parseInt(r.charAt(i), 10);
    if (i % 2 != 0)
      c *= 2;
    t = t + c;
  }

  // Finally, add up all the single digits in this string.

  n = 0;
  for (i = 0; i < t.length; i++) {
    c = parseInt(t.charAt(i), 10);
    n = n + c;
  }

  // If the resulting sum is an even multiple of ten (but not zero), the
  // card number is good.

  if (n != 0 && n % 10 == 0)
    return true;
  else
    return false;
}