// JavaScript Document

// Simple function to demonstrate Left Nav Rollovers for templates only.
// NCR will need to implement in existing CMS system

function leftNavColorToggle(idCell, newStyle) {
	document.getElementById(idCell).className = newStyle;
	document.getElementById("a_"+idCell).className = newStyle;	
}

// Simple function to demonstrate Main (Top/Global) Nav "on" states for templates only. 
// This script is used to assist displaying the "on" states of the current
// section of the site in the Main Nav so that multiple versions of the header
// did not need to be created. Ideally, NCR will be able to implement this 
// type of feature with their existing CMS system.


function setMainNav(sectionName, onStyle) {
	//document.getElementById("section_"+sectionName).className = onStyle;
}

var randomParamProduct = 1;
var randomParamSolution = 1;

function psModule(url,target,direction,maxSize,theModule) {
    if(direction == 'forward') {
        if(theModule == 'product') {
            randomParamProduct++;
            if(randomParamProduct > maxSize)
                randomParamProduct = 0;
        }
        else {
            randomParamSolution++;
            if(randomParamSolution > maxSize)
                randomParamSolution = 0;
        } 
    }
    else {
        if(theModule == 'product') {
            randomParamProduct--;
            if(randomParamProduct < 0)
                randomParamProduct = maxSize;
        }
        else {
             randomParamSolution--;
            if(randomParamSolution < 0)
                randomParamSolution = maxSize;
        }
    }
    if(theModule == 'product')
        ajax(url,target,randomParamProduct,theModule);
    else
        ajax(url,target,randomParamSolution,theModule);
        
       
}

function ajax(url,target,randomParam,theModule) {
    // native XMLHttpRequest object
   document.getElementById(target).innerHTML = 'sending...';
   url = url + '?currentCount=' + randomParam + '&ps=' + theModule;
   if (window.XMLHttpRequest) {
       req = new XMLHttpRequest();
       req.onreadystatechange = function() {ajaxDone(target);};
       req.open("GET", url, true);
       req.send(null);
   // IE/Windows ActiveX version
   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
       if (req) {
           req.onreadystatechange = function() {ajaxDone(target);};
           req.open("GET", url, true);
           req.send(null);
       }
   }
}

function ajaxFirstCall(url,target,randomParam) {
    // native XMLHttpRequest object
   document.getElementById(target).innerHTML = 'sending...';
   if (window.XMLHttpRequest) {
       req = new XMLHttpRequest();
       req.onreadystatechange = function() {ajaxDone(target);};
       req.open("GET", url, true);
       req.send(null);
   // IE/Windows ActiveX version
   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
       if (req) {
           req.onreadystatechange = function() {ajaxDone(target);};
           req.open("GET", url, true);
           req.send(null);
       }
   }
  // setTimeout("ajax('/includes/homepromo_products.jsp?inuse=y','scriptoutput',0,'product')",700);
}


function ajaxDone(target) {
    // only if req is "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200 || req.status == 304) {
            results = req.responseText;
            document.getElementById(target).innerHTML = results;
        } else {
            document.getElementById(target).innerHTML="ajax error:\n" +
            req.statusText;
       }
   }
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// contact form validation
function checkForm(frm) {

var message = "The following fields are required:\n\n";
var focusField = 0;

if(frm.country.value == "") {
	//send focus to first blank field
	if(message == "The following fields are required:\n\n")
		focusfield = 1;
	message += " - Country\n";
}

if (frm.country.value == "United States") {
	if (frm.state.value == "") {
		if(message == "The following fields are required:\n\n")
		focusfield = 1;
		message += " - State\n";
	}
	
}

if (frm.country.value == "Canada") {
	if (frm.province.value == "") {
		if(message == "The following fields are required:\n\n")
		focusfield = 1;
		message += " - Province\n";
	}
	
}

if(frm.fName.value == "") {
	//send focus to first blank field
	if(message == "The following fields are required:\n\n")
		focusfield = 3;
	message += " - First Name\n";
}

if(frm.lName.value == "") {
	//send focus to first blank field
	if(message == "The following fields are required:\n\n")
		focusfield = 3;
	message += " - Last Name\n";
}

// Check for Contact Method Radion Button
checked = false;
cmethod = "";
for (i = 0; i <2; i++) {
	if (frm.cmethod[i].checked) {
	cmethod = frm.cmethod[i].value;
	checked = true;
	}
}
if (!checked) {
	//send focus to first blank field
	if(message == "The following fields are required:\n\n")
		focusfield = 3;
	message += " - Preferred Contact Method\n";
}
else { // Now check values
	if( cmethod == "phone" && frm.phone.value == "") {
		//send focus to first blank field
		if(message == "The following fields are required:\n\n")
			focusfield = 3;
		message += " - Phone\n";
	}
	if( cmethod == "email" && frm.email.value == "") {
		//send focus to first blank field
		if(message == "The following fields are required:\n\n")
			focusfield = 3;
		message += " - Email Address\n";
	}

}

if(frm.company.value == "") {
	//send focus to first blank field
	if(message == "The following fields are required:\n\n")
		focusfield = 3;
	message += " - Company\n";
}


if(message == "The following fields are required:\n\n") {
	return true;
}
else {
	alert(message);
	return false;
   }
}

function IsNumeric(sText) {
var ValidChars = "0123456789.";
var IsNumber=true;
var Char;

for (i = 0; i < sText.length && IsNumber == true; i++) 
  { 
  Char = sText.charAt(i); 
  if (ValidChars.indexOf(Char) == -1) 
     {
     IsNumber = false;
     }
  }
return IsNumber;
}

function checkEmail(val) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(val))
        return (true);
    else
        return (false);
}

function showStateProv(currField) {
	var currValue=currField;
	if (currValue == "United States" ) {
		document.getElementById('provincerow').style.display="none";
		document.getElementById('staterow').style.display = "";
	}
	else if ( currValue == "Canada" ) {
		document.getElementById('staterow').style.display = "none";
		document.getElementById('provincerow').style.display = "";
		showOtherFields(currValue);
	}
	else { // Make sure state and province hidden
		document.getElementById('staterow').style.display = "none";
		document.getElementById('provincerow').style.display="none";
		showOtherFields(currValue);
	}
	
	

}

function showOtherFields(callLoc) {
	
	var currValue = callLoc;
	// Make all other rows visible
	document.getElementById('firstrow').style.display = "";
	// Toggle Surname / Last Name label
	if (currValue == "United States" || currValue == "Canada" || currValue == "State") {
		document.getElementById('surN').style.display = "none";
		document.getElementById('lastN').style.display = "";
	}
	else {
		document.getElementById('lastN').style.display = "none";
		document.getElementById('surN').style.display = "";
	}
	
	document.getElementById('lastrow').style.display = "";
	document.getElementById('contactrow').style.display = "";
	document.getElementById('phonerow').style.display = "";
	document.getElementById('emailrow').style.display = "";
	document.getElementById('companyrow').style.display = "";
	document.getElementById('solutiontitlerow').style.display = "";
	document.getElementById('solutionrow').style.display = "";
	document.getElementById('commentsrow').style.display = "";
	
}

function showContact(value) {
	var contactM = value;
	if (contactM == "email" ) {
		document.getElementById('pNotReq').style.display = "";
		document.getElementById('pReq').style.display = "none";
		document.getElementById('eNotReq').style.display = "none";
		document.getElementById('eReq').style.display = "";
	}
	else if (contactM == "phone" ) {
		document.getElementById('pNotReq').style.display = "none";
		document.getElementById('pReq').style.display = "";
		document.getElementById('eNotReq').style.display = "";
		document.getElementById('eReq').style.display = "none";
	}
}
