function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function isNetScape(){
 	var agt=navigator.userAgent.toLowerCase();
	var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
   // alert(is_nav);
    return is_nav;
}

function getBrowserVersion(){
  return parseInt(navigator.appVersion);
}

function whatsthispopup(evt,vistype, divId) {
     menu = document.getElementById(divId);
     if ( isNetScape()){
     	menu.style.visibility=vistype;
     	menu.style.left=evt.pageX-200;
     	menu.style.top=evt.pageY+25;
     }else{
     	menu.style.visibility=vistype;
     	menu.style.left=document.body.scrollLeft+event.clientX-200; //event.offsetX+10;
     	menu.style.top=document.body.scrollTop+event.clientY+25; //event.offsetY+15;
     }
}
function whatsthispopupProfiles(evt,vistype) {
     menu = document.getElementById("whatsthisandor");
     if ( isNetScape()){
     	menu.style.visibility=vistype;
     	menu.style.left=evt.pageX-200;
     	menu.style.top=evt.pageY+25;
     }else{
	     menu.style.visibility=vistype;
	     menu.style.left=document.body.scrollLeft+event.clientX-200; //event.offsetX+10;
	     menu.style.top=document.body.scrollTop+event.clientY+25; // -event.offsetY+20;
	 }
}

function whatsthispopupSubCategory(evt,vistype) {
     menu = document.getElementById("whatsthisandor");
     if ( isNetScape()){
     	menu.style.visibility=vistype;
     	menu.style.left=evt.pageX-225;
     	menu.style.top=evt.pageY+25;
     }else{
     	menu.style.visibility=vistype;
     	menu.style.left=document.body.scrollLeft+event.clientX-225; // -event.offsetX+400;
     	menu.style.top=document.body.scrollTop+event.clientY+25; //-event.offsetY+20;
     }
}

function replaceComma(s) {
	var sAct = "";
  for (var i = 0; i < s.length; i++) {
  	if(s.charAt(i) == ","){
  	
  	}else{
  		sAct = sAct + s.charAt(i);
  	}
  }
  return sAct;
}

function openPlainWindow(url,w,h,l,t) {	
	att_string = "width="+w+",height="+h+"left="+l+",top="+t+",resizable=1,status=0,location=0,toolbar=0,menu=0,scrollbars=1";	
	var newwin = open(url,"child_win",att_string);
	newwin.document.close();
	newwin.focus();	
}

function openFullWindow(url,w,h,l,t) {
	att_string = "width="+w+",height="+h+"left="+l+",top="+t+",resizable=1,status=0,location=0,toolbar=0,menu=0,scrollbars=1";
	return window.open(url,"_blank", "titlebar=0");
}

// Initiate progress dialog.
function ProgInit() {
    document.body.style.cursor = "wait";
    
	ProgressCreate(900);
}

// Create and display the progress dialog.
// end: The number of steps to completion
function ProgressCreate(end) {
	//alert('progress');
	// Move layer to center of window to show
	if (document.all) {	// Internet Explorer
		document.all.maindiv.style.visibility="hidden";
		progress.className = 'show';
		progress.style.left = (document.body.clientWidth/2) - (progress.offsetWidth/2);
		progress.style.top = document.body.scrollTop+(document.body.clientHeight/2) - (progress.offsetHeight/2);
	} else if (document.layers) {	// Netscape
		document.maindiv.visibility=false;
		document.progress.visibility = true;
		document.progress.left =  100; //window.innerWidth;
		document.progress.top = pageYOffset; //+(window.innerHeight/2) - 40;
	} else if (document.getElementById) {	// Netscape 6+
		document.getElementById("maindiv").className='hide';
		document.getElementById("progress").className = 'show';
		document.getElementById("progress").style.left = 100;
		document.getElementById("progress").style.top = pageYOffset;
	}
}



// Check Login
function submitLogin(){
	var email = trim(document.loginfrm.txtEmail.value);
	var pwd = trim(document.loginfrm.txtPwd.value);
	var pgfrom = trim(document.loginfrm.pagefrom.value);
	var prdct = trim(document.loginfrm.product.value);
	document.loginfrm.currentpage.value = document.frm.currentpage.value;

	if(email == ""){
		alert("Please enter you email id as your login name");
		document.loginfrm.txtEmail.focus();
		return;
	}
	else if(pwd == ""){
		alert("Please enter your password");
		document.loginfrm.txtPwd.focus();
		return;
	}
	document.loginfrm.type.value = "login";
	document.loginfrm.action =  "/profiles/servlet/Main";
	document.loginfrm.submit();
}

function logoutTP(){
	document.loginfrm.type.value = "logout";
	
	document.loginfrm.action =  "/profiles/servlet/Main";
	document.loginfrm.submit();
	
}

function myProfiles(){
	document.loginfrm.page.value = "myprofiles";
	document.loginfrm.action =  "/profiles/servlet/Profiles";
	document.loginfrm.submit();

}

function mySearches(){
	document.loginfrm.page.value = "mysearches";
	document.loginfrm.action =  "/profiles/servlet/Profiles";
	document.loginfrm.submit();
}

function myAccount(){
	document.loginfrm.page.value = "myaccount";
	document.loginfrm.action =  "/profiles/servlet/Profiles";
	document.loginfrm.submit();
}

function goToLandingPage(landingPageAction){
	if (landingPageAction == "piershome") {
	   if (!confirm("WARNING: This will log you out of the application.  Click OK to continue.")) return;
	}
	document.loginfrm.type.value = "landingPage";
	document.loginfrm.landingPageAction.value = landingPageAction;
	document.loginfrm.action =  "/profiles/servlet/Main";
	document.loginfrm.submit();
}

function submitToWebTi(webTiUrl){
	document.loginfrm.page.value = "login";
	document.loginfrm.action = webTiUrl + "/webti/servlet/Main";
	document.loginfrm.submit();
}

function runMySearch(srchId, srchType, srchName, type){

	var pagefrom = "";
	if(type == "run")pagefrom = "runmysearch";
	else if(type == "edit")pagefrom = "editmysearch";
	else if(type == "del")pagefrom = "delmysearch";
	
	//var check = 0;
	//var rowCnt = -1;
	var val = "";
	
	/*if(count > 1){
		for(var i=0; i < document.frm.chkMySrch.length; i++){
			if(document.frm.chkMySrch[i].checked == true){
				check = check + 1;
				rowCnt = i;

			}
		}
		if(rowCnt != -1){
			val = document.frm.chkMySrch[rowCnt].value;
		}
		
	}else{
		if(document.frm.chkMySrch.checked == true){
			check = 1;
		}
		val = document.frm.chkMySrch.value;
	}
	if(check == 0){
		alert("Please check one Search Name to submit");
		return false;
	}
	
	if(check > 1 && type != "del"){
		alert("You are allowed to select only one search to Run or Edit");
		return false;
	}
	
	var arr = val.split(',');
	
	document.frm.pagefrom.value = pagefrom;

	if(type != "del"){
		document.frm.srchId.value = arr[0];
		document.frm.srchType.value = arr[1];
		document.frm.srchName.value = arr[2];
	}
	document.frm.delCnt.value = check;*/
	
	document.frm.pagefrom.value = pagefrom;
	document.frm.srchId.value = srchId;
	document.frm.srchType.value = srchType;
	document.frm.srchName.value = srchName;
	
	document.frm.action = "/profiles/servlet/Search";
	document.frm.submit();
}

function valSpecialChars(str){

	var ret = true;
	var iChars = "!#@$%^&*()+=[]\\\;,/{}|\":<>?";
        for (var i = 0; i < str.length; i++) {
        	if (iChars.indexOf(str.charAt(i)) != -1) {

            	//alert ("The entered value have special characters. \nPlease remove the special characters and try again.\n");
                ret = false;
                break;
        	}

		}
		return ret;
}

function valEmailSpecialChars(str){

	var ret = true;
	//var iChars = "!#$%^&*()+=-[]\\\';,/{}|\":<>?";
	var iChars = "!#$%^&*()+=-[]\\\';/{}|\":<>?";
        for (var i = 0; i < str.length; i++) {
        	if (iChars.indexOf(str.charAt(i)) != -1) {

            	//alert ("The entered value have special characters. \nPlease remove the special characters and try again.\n");
                ret = false;
                break;
        	}

		}
		return ret;
}

function backToSearch(val){
	if(val == "lookup"){
		document.frm.page.value = "lookup";
		//document.frm.pagefrom.value = "results";
	}else{
		document.frm.page.value = "advanced";
		//document.frm.pagefrom.value = "results";
	}
	document.frm.action =  "/profiles/servlet/Profiles";
	document.frm.submit();
}

function toggleMiles()
{
	if( document.frm.milesChkbox.checked==false )
	{
		document.frm.txtRadius.disabled = true ;	
		document.frm.txtRadius.selectedIndex = 0 ;
	}
	if( document.frm.milesChkbox.checked==true )
	{
		document.frm.txtRadius.disabled = false ;	
	}	
}



function backToResults(){
	var httpUrl = document.frm.httpUrl.value;
	document.frm.page.value = "backtoresults";
	document.frm.action =  "/profiles/servlet/Results";
	document.frm.submit();
}

function newPopUp(url,winname,w,h) {
	window.open(url,winname,'width=' + w + ',height=' + h + ',left=100,top=100,resizable=1,status=0,location=0,toolbar=0,menu=0,scrollbars=1')
}

function openForgotPwd(){
	document.loginfrm.action =  "/profiles/jsp/ForgotPassword.jsp";
	document.loginfrm.submit();
}

function showRateCard(){
	document.loginfrm.page.value = "ratecard";
	document.loginfrm.action =  "/profiles/servlet/Profiles";
	document.loginfrm.submit();

}

function dataStats(){
	document.loginfrm.page.value = "datastats";
	document.loginfrm.action =  "/profiles/servlet/Profiles";
	document.loginfrm.submit();
}

function openPriceCard(){
	var url =  "/profiles/servlet/Profiles?page=ratecard&display=popup";
	window.open(url, 'ratecard', 'width=600,height=400,left=100,top=100,resizable=0,status=0,location=0,toolbar=0,menu=0,scrollbars=1')
}

function goToCart(){
	document.frm.type.value = "backtocart";
	document.frm.pagefrom.value = document.frm.currentpage.value;
	document.frm.action =  "/profiles/servlet/Cart";
	document.frm.submit();
}

function openSample(){
	
	url = "/profiles/jsp/reports/ProfileReport_Sample.pdf";
	window.open(url, "winprof", "location=yes,menubar=yes,status=no,toolbar=no,scrollbars=yes,resizable=yes,width=800,height=450");

}

function download(){
	var var1 = document.frm.detailobj.value;
	var url = "/profiles/jsp/ReportWheel.jsp?page=export&exportfrom=bol&detailobj="+var1;
	window.open(url,"reportwin","location=no,menubar=yes,status=no,toolbar=no,scrollbars=no,resizable=yes,width=740,height=600,top=100,left=100");
}

function isValidEmail(str) {

	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	
	if (str.indexOf(at) == -1){//if an @ is missing
		alert("Invalid E-mail ID. Please enter a valid E-mail ID.");
		return false;
	}
	
	//if an @ is missing or at the beginning or end
	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr){
		alert("Invalid E-mail ID. Please enter a valid E-mail ID.");
		return false;
	}
	
	//if a dot is missing, or at the beginning or end
	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr){
		alert("Invalid E-mail ID. Please enter a valid E-mail ID.");
		return false;
	}
	
	/*if (str.indexOf(at,(lat+1)) != -1){//if you find an @ after the first one
		alert("Invalid E-mail ID. Please enter a valid E-mail ID.");
		return false;
	}*/
	
	if (str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot){
		alert("Invalid E-mail ID. Please enter a valid E-mail ID.");
		return false;
	}
	
	if (str.indexOf(dot,(lat+2)) == -1){
		alert("Invalid E-mail ID. Please enter a valid E-mail ID.");
		return false;
	}
			
	if (str.indexOf(" ") != -1){//if there is a space
		alert("Invalid E-mail ID. Please enter a valid E-mail ID.");
		return false;
	}
	
	return true;				
}

function isNumber(str){

	var ret = true;
	var validChars = "0123456789";
        for (var i = 0; i < str.length; i++) {
        	if (validChars.indexOf(str.charAt(i)) == -1) {
                ret = false;
                break;
        	}
		}
		return ret;
}
