var postdataStart = '<?xml version="1.0" encoding="utf-8"?>\n<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\n<soap:Body>\n';
var postdataEnd = '</soap:Body>\n</soap:Envelope>\n';

if (!document.getElementById) 
{ 
      window.location = "/noncompliant.html"; 
} 

/*
	Functions that are used throughout the application. This
	file should always be included.
*/

// Will display errorMsg in the errorElmId's innerHTML.
// errorElmId should be a DIV of class type error
function displayInputError(errorMsg, errorElmId){	
	errorElm = document.getElementById(errorElmId);
	errorElm.innerHTML = errorMsg;
}

function removeErrorMessage(errorElmId){
	errorElm = document.getElementById(errorElmId);
	errorElm.innerHTML = null;
}

// Validate a textbox meets the required Length
function validateTextBox(textBoxId, requiredLen){
	alert( textBoxId );
	var textBox = document.getElementById(textBoxId);
	var returnValue = false;
	//alert('validating: ' + textBoxId);
	if( textBox.value.length >= requiredLen ){
		returnValue = true;
	}
	
	return returnValue;
}

// Be sure a drop down selectedIndex is non negative
function validateDropDown(dropDownId){
	var returnValue = false;
	var dropDown = document.getElementById(dropDownId);
	
	if(dropDown.selectedIndex >= 1){
		returnValue = true
	}else{
		//alert(dropDownId + ' failed');
	}
	
	return returnValue;
}

// Removes the white space
function removeWhiteSpace(theElmId){
	var theElm = document.getElementById(theElmId);
	var regExp = /\W/g;
	var cleanString;
	var valueString;
	
	valueString = theElm.value;
	cleanString = valueString.replace(regExp, '');
}

// Remove all non char and int values
function removeCharacters(theElmId){

	var theElm = document.getElementById(theElmId);
	var cleanRegExp = /\D\W/g;
	var cleanString;
	var valueString = theElm.value;
	
	cleanString = valueString.replace(cleanRegExp, '');
	theElm.value = cleanString;
}

// Remove all chars from a textbox
function cleanNumberField(theElmId){
	var theElm = document.getElementById(theElmId);
	var numRegExp = /\D/g; // Look for any non-digit ( globally - throughout the field )
	var cleanString; // This will hold the return of the replace.
	var valueString = theElm.value; // the value of the textbox
	cleanString = valueString.replace(numRegExp, ''); // Doesn't change the field value, returns a clean copy of the clean field value
	theElm.value = cleanString; // now set the value of the textbox with the clean value
}

/** EMAIL VALIDATION **/

// Email Validation. Written by PerlScriptsJavaScripts.com
// http://www.perlscriptsjavascripts.com/js/check_email.html
function check_email(e) {
    var ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
    var retValue = true
    var re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    var re_two =  /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

    for(i=0; i < e.length ;i++){
        if(ok.indexOf(e.charAt(i))<0){ 
            return false;
        }
    }

    if (!(e.match(re)) && (e.match(re_two))) {
        return true;		
    }else{
        return false;
    }

}
/* POPUP FUNCTIONALITY */
function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,re  sizable=0,width=600,height=440,left = 340,top = 292');");
}

function helpPopUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=340,left = 340,top = 342');");
}

function videoPopUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,re  sizable=0,width=600,height=340,left = 340,top = 292');");
}


/* This function is used on the EmailControl. The reason 
   it is here.. simple: The EmailControl can be used all over 
   the application, and this file is included ( always ) */

function validateEmailControl(){
	var name = document.getElementById('tbUsersName');
	var sender = document.getElementById('tbSender');
	var subjuct = document.getElementById('tbSubject');
	var body = document.getElementById('tbBody');
	var returnValue = true;
	
	//alert('I am validating.');
	
	if((validateTextBox('emailControl3_tbUsersName', 1)) == false){		
		returnValue = false;
	}
	
	if((validateTextBox('emailControl3_tbSender', 1)) == false){		
		returnValue = true;
	}
	
	if((validateTextBox('emailControl3_tbSubject', 1)) == false){		
		returnValue = false;
	}
	
	if((validateTextBox('emailControl3_tbBody', 5)) == false){		
		returnValue = false;
	}

	return false;
}


//bind textbox to a button for the enter key
function enterSubmitFix(buttonToSubmit, e)
{
	if((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) 
	{
		var button = document.getElementById(buttonToSubmit.split(":").join("_"));
		if(button.type == "button" || button.type == "image" || button.type == "submit")
		{
			button.focus();		
			document.forms[0].elements["__EVENTTARGET"].value = buttonToSubmit;
			document.forms[0].elements["__EVENTARGUMENT"].value = "";
			document.forms[0].submit();
		}
		else
		{
			__doPostBack(buttonToSubmit.split(":").join("$"), '')
		}
		return false;
	} 
	else 
	{
		return true;
	}
}



function validateCustomerComment( emailElmId, commentElmId ) {
    var txtEmail = document.getElementById(emailElmId);
    var txtComment = document.getElementById(commentElmId);
    var valError = document.getElementById('validationError');
    var emailIsValid = false;
    var commentIsValid = false;
    var isValid = true;
    var displayMessage = '';
    
    if( txtEmail == null ) {
        alert('Unable to get email with id ' + emailElmId);
        return false;
    }
    
    if( txtComment == null ) {
        alert('Unable to get comment with id ' + txComment);
        return false;
    }
        
    emailIsValid = check_email(txtEmail.value);
    if( emailIsValid == false )
        displayMessage = 'Please enter a properly formatted email address.<br />';
        
    commentIsValid = txtComment.value.length > 0;
    if( commentIsValid == false)
        displayMessage += 'Please enter a comment.';
    
    if( emailIsValid == false || commentIsValid == false ) {
		valError.style.display = 'block';
        valError.innerHTML = displayMessage;
        isValid = false;
    }
    return isValid;
}