/* GLOBAL FUNCTIONS (not webapp specific)
/* ------------------------------------------------ */

function launch(newURL, newName, newFeatures, orgName) {
    var remote = open(newURL, newName, newFeatures);
    if (remote.opener == null)
        remote.opener = window;

    remote.opener.name = orgName;
    return remote;
}

function popWindow(url, sb, rz, w, h, mn, tl, tt) {
    if (mn == null) {
        mn = 'no';
    }
    if (tl == null) {
        tl = 'no';
    }
    if (tt == null) {
        tt = 'pop_window';
    }

    myRemote = launch(url,
            tt,
            "toolbar=" + tl + ",status=no,menubar=" + mn + ",scrollbars=" + sb + ",resizable=" + rz + ",width=" + w + ",height=" + h,
            "parent_opener");
}

function closeWindow() {
    window.close();
}


function newWindow(url) {
    myRemote = launch(url,
            'pop_window',
            "toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,location=no",
            "parent_opener");
}

function winPopup(sUrl, sWidth, sHeight) {
    agent = navigator.userAgent;
    windowName = "Sitelet";
    params = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,height=" + sHeight + ",width=" + sWidth;
    win = window.open(sUrl, windowName, params);
    if (agent.indexOf("Mozilla/2") != -1 && agent.indexOf("Win") == -1) {
        win = window.open(sUrl, windowName, params);
    }
    if (!win.opener) {
        win.opener = window;
    }
}

function openFlashFile(newURL, width, height, left, top) {
    var defaultWidth = 640;
    var defaultHeight = 494;
    var defaultLeft = 100;
    var defaultTop = 100;

    width = width == null ? defaultWidth : width;
    height = height == null ? defaultHeight : height;
    left = left == null ? defaultLeft : left;
    top = top == null ? defaultTop : top;

    window.open(newURL, "_blank", 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',resizable=no,location=no,directories=no,scrollbars=no,menubar=no,status=no,toolbar=no,titlebar=no,hotkeys=no');
}

function toggle_visibility(id) {
   var e = document.getElementById(id);
	//alert("[" + e.style.display + "]");
	if (e.style.display == '') {
		e.style.display = 'none';
	} else {
		e.style.display = '';
	}
}

function getElementById(id) {
    if (typeof id == 'string') {
        return document.getElementById(id);
    }
    return eval(id);
}

function getElementsByTagName(name) {
    if (typeof name == 'string') {
        return document.getElementsByTagName(name);
    }
    return eval(name);
}


function hide(id) {
    getElementById(id).style.display = 'none'
}

function show(id) {
    getElementById(id).style.display = ''	;
}

function toggle(id) {
    eval((getElementById(id).style.display != 'none') ? 'hide(id)' : 'show(id)');
}

function landTo(id) {
    window.scrollTo(getElementById(id).offsetLeft,getElementById(id).offsetTop);
}


// Common functions used by REGISTRATION SURVEY -> starts

// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}

// Checks whether passed value is integer or not.
function isInteger(s) {
	var i;
	for (i = 0; i < s.length; i++) {   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

// This function searches each and every characters of string1 in string2
// and returns a string of characters which are not in string2.
function stripCharsInBag(str1, str2) {   
	var i;
	var returnString = "";
	// Search through string's characters one by one. If character is not in bag, append to returnString.
	for (i = 0; i < str1.length; i++) {
		// Check that current character isn't whitespace.
		var c = str1.charAt(i);
		if (str2.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}


// This function checks whether phone number value is not blank and calls the function
// checkInternationalPhone to validate phone number.
function validatePhone(phoneValue){
	if( phoneValue != null && trim(phoneValue).length > 0 ) {
		if (!checkInternationalPhone(phoneValue)){
			return false;
		}
	} else {
		return false;
	}
	return true;
}

// Declaring required variables for local/internation phone validation
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

// This function is used to verify whether the given phone value is a possible 
// valid international phone number 
function checkInternationalPhone(strPhone) {
	var bracket=3;
	strPhone=trim(strPhone);
	if(strPhone.indexOf("+")>1) {
		return false;
	}
	if(strPhone.indexOf("-")!=-1) {
		bracket=bracket+1;
	}
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket) {
		return false;
	}
	var brchr=strPhone.indexOf("(");
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")") {
		return false;
	}
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1) {
		return false;
	}
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}




// Registration survey common functions -> ends



// Moved from b2b_function.js -> starts
function validateDate(lsStringDate,loItem){

	var today= new Date();
    var day= today.getDate();
    var month= today.getMonth() + 1;
    var year= today.getFullYear();

	var lsDate = "";
	// CHECK FOR MISSING DATA
	if((lsStringDate=='') || (lsStringDate==null) || (lsStringDate =='undefined')){
		loItem.value = "";
		alertDate(loItem);
		return false;
	}

	var lsStringCounter = lsStringDate.length;
	var liDashCounter = 0;
	for(var x=0;x<lsStringDate.length;x++){
		if(lsStringDate.charAt(x) == "/"){
			liDashCounter++;
		}
	}

	if(liDashCounter != 2){
		loItem.value = "";
		alertDate(loItem);
		return false;
	}

	var loTemp = lsStringDate.split("/");
	var mm = loTemp[0];
	var dd = loTemp[1];
	var yyyy = loTemp[2];

	if(mm == null || dd == null || yyyy == null){
		loItem.value = "";
		alertDate(loItem);
		return false;
	}

//	alert(dd+mm+yyyy);
	var valid = true;

	//Fix the year to a yyyy format
	if(yyyy.length == 1){
		yyyy = '200' + yyyy;
	}

	if(yyyy.length == 2) {//user only entered a 2 digit year
		//convert to number
		yyyy = yyyy -0;

		// I chose 81 as a cutoff because TAIS started in 81
		if(yyyy>81) {
			yyyy = '19' + yyyy;
		} else {
			if(yyyy==0){//00 is interpreted as 0
				yyyy = '2000';
			}else if (yyyy<10){//01-09
				yyyy = '200' + yyyy;
			}else{
				yyyy = '20'+ yyyy;
			}
		}
	}

	if (yyyy.length < 4) {
		valid = false;
	}

	yyyy = yyyy -0;//convert it back to number

	valid = valid && (mm >= 1 && mm <= 12);
	if (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12)
		{
			valid = valid && (dd >= 1 && dd <= 31);

		}
	else if (mm == 2)
	{
		if (yyyy%4 == 0) {//leap year possible
			if(yyyy%100 == 0) {
				if(yyyy%400 == 0) {
					valid = valid && (dd >= 1 && dd <= 29);
				} else {
					valid = valid && (dd >= 1 && dd <= 28);
				}
			} else {
				valid = valid && (dd >= 1 && dd <= 28);
			}
		} else {
			valid = valid && (dd >= 1 && dd <= 28);
		}
	} else {//30 day month
		valid = valid && (dd >= 1 && dd <= 30);
	}

	if(yyyy < year)
		valid = false;

	if(yyyy == year){
		if(mm < month){
			valid = false;
		}
		if(mm == month){
			if(dd < day){
			valid = false;
	 		}
	 	}
	}

	//month needs to be 06 not 6
	if(mm < 10){
		if(mm.length == 1){
			mm = '0' + mm;
		}
	}

	if(dd < 10){
		if(dd.length == 1){
			dd = '0' + dd;
		}
	}

	if(valid){
		lsDate = mm + '/' + dd + '/' + yyyy;
		loItem.value=lsDate;
		return true;
	} else {
		alertDate(loItem);
		return false;
	}
}

// This is function is basically the same as the one in the common functions.
// The only difference is, in this function, dates before and after the current date
// are allowed.
function validateAnyDate(lsStringDate,loItem){

    var today= new Date();
    var day= today.getDate();
    var month= today.getMonth() + 1;
    var year= today.getFullYear();

	var lsDate = "";
	// CHECK FOR MISSING DATA
	if((lsStringDate=='') || (lsStringDate==null) || (lsStringDate =='undefined')){
		loItem.value = "";
		alertDate(loItem);
		return false;
	}

	var lsStringCounter = lsStringDate.length;
	var liDashCounter = 0;
	for(var x=0;x<lsStringDate.length;x++){
		if(lsStringDate.charAt(x) == "/"){
			liDashCounter++;
		}
	}

	if(liDashCounter != 2){
		loItem.value = "";
		alertDate(loItem);
		return false;
	}

	var loTemp = lsStringDate.split("/");
	var mm = loTemp[0];
	var dd = loTemp[1];
	var yyyy = loTemp[2];

	if(mm == null || dd == null || yyyy == null){
		loItem.value = "";
		alertDate(loItem);
		return false;
	}

//	alert(dd+mm+yyyy);
	var valid = true;

	//Fix the year to a yyyy format
	if(yyyy.length == 1){
		yyyy = '200' + yyyy;
	}

	if(yyyy.length == 2) {//user only entered a 2 digit year
		//convert to number
		yyyy = yyyy -0;

		// I chose 81 as a cutoff because TAIS started in 81
		if(yyyy>81) {
			yyyy = '19' + yyyy;
		} else {
			if(yyyy==0){//00 is interpreted as 0
				yyyy = '2000';
			}else if (yyyy<10){//01-09
				yyyy = '200' + yyyy;
			}else{
				yyyy = '20'+ yyyy;
			}
		}
	}

	if (yyyy.length < 4) {
		valid = false;
	}

	yyyy = yyyy -0;//convert it back to number

	valid = valid && (mm >= 1 && mm <= 12);
	if (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12)
		{
			valid = valid && (dd >= 1 && dd <= 31);

		}
	else if (mm == 2)
	{
		if (yyyy%4 == 0) {//leap year possible
			if(yyyy%100 == 0) {
				if(yyyy%400 == 0) {
					valid = valid && (dd >= 1 && dd <= 29);
				} else {
					valid = valid && (dd >= 1 && dd <= 28);
				}
			} else {
				valid = valid && (dd >= 1 && dd <= 28);
			}
		} else {
			valid = valid && (dd >= 1 && dd <= 28);
		}
	} else {//30 day month
		valid = valid && (dd >= 1 && dd <= 30);
	}

	/*if(yyyy < year)
		valid = false;

	if(yyyy == year){
		if(mm < month){
			valid = false;
		}
		if(mm == month){
			if(dd < day){
			valid = false;
	 		}
	 	}
	} */

	//month needs to be 06 not 6
	if(mm < 10){
		if(mm.length == 1){
			mm = '0' + mm;
		}
	}

	if(dd < 10){
		if(dd.length == 1){
			dd = '0' + dd;
		}
	}

	if(valid){
		lsDate = mm + '/' + dd + '/' + yyyy;
		loItem.value=lsDate;
		return true;
	} else {
		alertDate(loItem);
		return false;
	}
}


function alertDate(loItem){
	loItem.focus();
	alert('Please enter a valid date \n in mm/dd/yyyy format');
}

// Moved from b2b_function.js -> ends


function isBrowserIE6(){
	if($.browser.msie && parseFloat($.browser.version) < 7){
	//do other stuff
		return true;
	} else {
		return false;
	}
}

function numericOnKeyPress(e) {
	// Allow tabs, delete, backspace in Firefox
	if (e.which != null && (e.which == 0 || e.which == 8)) {
		//alert ("e.which: " + e.which);
		return true;
	}
	return checkOnKeyPress(e, /\d/);
}

function checkOnKeyPress(e, numcheck) {
    evt = e || window.event;
    var keynum = evt.which || evt.keyCode;

    var keychar = String.fromCharCode(keynum)
	//alert ("keynum=" + keynum + ", keychar='" + keychar + "', numcheck=" + numcheck.toString() );
    return numcheck.test(keychar)
}




