// JavaScript Document
function validateLiterature(){
	//MM_validateForm('txtemail_address','','RisEmail','txtfirst_name','','R','txtlast_name','','R','txtstreet_address','','R','txtcity','','R','txtstate','','R','txtzip_code','','R','txtmessage','','R', 'txtcategory', '', 'RDD', 'product', '', 'RCB');
	var validateItem = new Array()
	var errorItem = new Array()
	
	validateItem[0] = validate('txtemail_address','','RisEmail');
	validateItem[1] = validate('txtfirst_name','','R');
	validateItem[2] = validate('txtlast_name','','R');
	validateItem[3] = validate('txtstreet_address','','R');
	validateItem[4] = validate('txtcity','','R');
	validateItem[5] = validate('txtstate','','R');
	validateItem[6] = validate('txtzip_code','','RisNum');	
	validateItem[7] = validateRadio();
	
	errorItem[0] 	= "- email address\n";
	errorItem[1] 	= "- first name\n";
	errorItem[2] 	= "- last name\n"
	errorItem[3] 	= "- street address\n"
	errorItem[4] 	= "- city\n"
	errorItem[5] 	= "- state\n"
	errorItem[6] 	= "- zip code\n"
	errorItem[7] 	= "- select your category\n"
	
	var errors;
	var errorStat 	= "Please correct the following:\n"
	var valid 		= true;
	for(var i=0;i<validateItem.length;i++){
		if(validateItem[i]){
			errorStat += errorItem[i];
			valid 	= false
		}
	}
	if(!valid){
		alert(errorStat);
	}
	return valid
}

function validate(){
  var i,p,q,nm,test,num,min,max,errors='',args=validate.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
  
  if (val) { nm=val.name.substring(3,val.name.length); if ((val=val.value)!="") {
    <!-- // if (val) { nm=val.name; if ((val=val.value)!="") { //-->
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- your email adress must contain an email address in this format:\n contact@siemens-home.com\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) return true;
	
}

function validateRadio(){
	var Q1Checked 

	for (var i=0; i<document.literature_request.Q1.length; i++) {	
		if (document.literature_request.Q1[i].checked) {
			Q1Checked = document.literature_request.Q1[i].value
		}
		
	}	
	
	if(!Q1Checked){ 
		return true;
	}else{
		return false;
	}	
}

function checkPhone(question){
	var arr = new Array();
	arr[0] = 0;
	arr[1] = 1;
	arr[2] = 2;
	arr[3] = 3;
	arr[4] = 4;
	arr[5] = 5;
	arr[6] = 6;
	arr[7] = 7;
	arr[8] = 8;
	arr[9] = 9;
	
	var arr2 = new Array();
	arr2[0]= '(';
	arr2[1] = ')';
	arr2[2] = '.';
	arr2[3] = ' ';
	
	
	var bullet = document.getElementById(question);	
	if(bullet.checked && bullet.value == "a"){
		Q2 = question+'a';
		Q2 = document.getElementById(Q2);
		
		var error = '';
		if(Q2.value == "" || Q2.value == null){
			error +='The following field(s) are required:\n';
			error +='- phone number is required\n';
		}
		if(error){
			alert(error);
			return false;
		}else{
			return true;
		}
	}
}


<!-- This script is based on the javascript code of Roman Feldblum (web.developer@programmer.net) -->
<!-- Original script : http://javascript.internet.com/forms/format-phone-number.html -->
<!-- Original script is revised by Eralper Yilmaz (http://www.eralper.com) -->
<!-- Revised script : http://www.kodyaz.com -->
//onkeydown="javascript:backspacerDOWN(this,event);" onkeyup="javascript:backspacerUP(this,event);"
 
var zChar = new Array(' ', '(', ') ', '-', '.');
var maxphonelength = 14;
var phonevalue1;
var phonevalue2;
var cursorposition;

function ParseForNumber1(object){
	phonevalue1 = ParseChar(object.value, zChar);
}
function ParseForNumber2(object){
	phonevalue2 = ParseChar(object.value, zChar);
}

function backspacerUP(object,e) {
	if(e){
		e = e
	} else {
		e = window.event
	}
	if(e.which){
		var keycode = e.which
	} else {
		var keycode = e.keyCode
	}
	
	ParseForNumber1(object)
	
	if(keycode >= 48){
		ValidatePhone(object)
	}
}

function backspacerDOWN(object,e) {
	if(e){
		e = e
	} else {
		e = window.event
	}
	if(e.which){
		var keycode = e.which
	} else {
		var keycode = e.keyCode
	}
	ParseForNumber2(object)
}

function GetCursorPosition(){
	
	var t1 = phonevalue1;
	var t2 = phonevalue2;
	var bool = false
	for (i=0; i<t1.length; i++){
		if (t1.substring(i,1) != t2.substring(i,1)) {
			if(!bool) {
				cursorposition=i
				bool=true
			}
		}
	}
}

function ValidatePhone(object){
	var p = phonevalue1
	p = p.replace(/[^\d]*/gi,"")
	
	if (p.length < 3) {
		object.value=p
	} else if(p.length==3){
		pp=p;
		d4=p.indexOf('(')
		d5=p.indexOf(') ')
		if(d4==-1){
		pp="("+pp;
	}
	if(d5==-1){
		pp=pp+") ";
	}
		object.value = pp;
	} else if(p.length>3 && p.length < 7){
		p ="(" + p;
		l30=p.length;
		p30=p.substring(0,4);
		p30=p30+") "
		
		p31=p.substring(4,l30);
		pp=p30+p31;
		
		object.value = pp;
	
	} else if(p.length >= 7){
		p ="(" + p;
		l30=p.length;
		p30=p.substring(0,4);
		p30=p30+") "
		
		p31=p.substring(4,l30);
		pp=p30+p31;
		
		l40 = pp.length;
		p40 = pp.substring(0,9);
		p40 = p40 + "-"
		
		p41 = pp.substring(9,l40);
		ppp = p40 + p41;
		
		object.value = ppp.substring(0, maxphonelength);
	}
	
	GetCursorPosition()
	
	if(cursorposition >= 0){
		if (cursorposition == 0) {
			cursorposition = 2
		} else if (cursorposition <= 2) {
			cursorposition = cursorposition + 1
		} else if (cursorposition <= 5) {
			cursorposition = cursorposition + 2
		} else if (cursorposition == 6) {
			cursorposition = cursorposition + 2
		} else if (cursorposition == 7) {
			cursorposition = cursorposition + 4
			e1=object.value.indexOf(') ')
			e2=object.value.indexOf('-')
			if (e1>-1 && e2>-1){
				if (e2-e1 == 4) {
					cursorposition = cursorposition - 1
				}
			}
		} else if (cursorposition < 11) {
			cursorposition = cursorposition + 3
		} else if (cursorposition == 11) {
			cursorposition = cursorposition + 1
		} else if (cursorposition >= 12) {
			cursorposition = cursorposition
	}
	
	var txtRange = object.createTextRange();
		txtRange.moveStart( "character", cursorposition);
		txtRange.moveEnd( "character", cursorposition - object.value.length);
		txtRange.select();
	}

}

function ParseChar(sStr, sChar){
	if (sChar.length == null)
	{
	zChar = new Array(sChar);
	}
	else zChar = sChar;
	
	for (i=0; i<zChar.length; i++)
	{
	sNewStr = "";
	
	var iStart = 0;
	var iEnd = sStr.indexOf(sChar[i]);
	
	while (iEnd != -1)
	{
	sNewStr += sStr.substring(iStart, iEnd);
	iStart = iEnd + 1;
	iEnd = sStr.indexOf(sChar[i], iStart);
	}
	sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);
	
	sStr = sNewStr;
	}

	return sNewStr;
}