function isUserExist(userId) {

	if(userId=='') {
		alert("Please enter the Member Id");
		return true;
	}	

//	var iChars = "!@#$%^&*()+=-[]\';,./{}|\":<>?";
	
//		if(!isMemberIdValid(userId)) {
//				alert("Member ID contains illegal characters. Please only use alphanumeric characters without spaces.");
//				return false;
//		}

 	var bag ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.";
	var fault = "";
	var right = "";
    for (i = 0; i < userId.length; i++)
    {
        var c = userId.charAt(i);
        if (bag.indexOf(c) == -1){
			fault = fault + c;
		}else{
			right = right + c;
		}
    }
	if(fault!=""){
		//if(fault.indexOf(' ')>0){
		//	alert("The member id you have chosen contains spaces. Please only use letters and numbers for your member id. You can also use underscore _ and fullstop");
		//}else{
			if(trim(fault)==''){ fault = "space "; } 
			alert("The member id you have chosen contains invalid characters: "+fault+" Please only use letters and numbers for your member id. You can also use underscore _ and fullstop");
			document.getElementById("memid").value = right ;
		//}
		return false;
	}
//The member id you have chosen contains invalid characters: ? >; ! Please only use letters and numbers for your member id. You can also use underscore _ and fullstop
//	for (var i = 0; i < userId.length; i++) {
//		if (iChars.indexOf(userId.charAt(i)) != -1) 
//		{
//			alert("Member ID contains characters that are not accepted. Please only use alphanumeric characters without spaces.");
//			return false;
//		}
//	}	

	if(userId.length<4) {
		alert("Member ID should be more than 4 characters");
		return false;
	}

	if(userId.length>20) {
		alert("Member ID should be less than 20 characters");
		return false;
	}

	http_request = false;
		if (window.XMLHttpRequest) {
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
			}
		} 
		else if (window.ActiveXObject) { // IE
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}

		if (!http_request) {
			alert('Giving up :( Cannot create an XMLHTTP instance');
			return false;
		}
	

	query = "userId="+userId;
	
	 url = 'isUserExist.php';
	//url = 'http://www.livewholesale.com/isUserExist.php';
		
		http_request.onreadystatechange = showResult;
		http_request.open('POST', url, true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send(query);
}
	
	function showResult() 
	{
			//alert(http_request.readyState);
				if (http_request.readyState == 4) 
			{
				if (http_request.status == 200) 
				{
					//alert(http_request.responseText);
					if(http_request.responseText=="<html>exists</html>") {
						document.getElementById("available").style.display='inline';
						document.getElementById("available").style.color='#c00';
						document.getElementById("available").innerHTML='Not Available!';
					}
					else {
						document.getElementById("available").style.color='';
						document.getElementById("available").style.display='inline';						
						document.getElementById("available").innerHTML='Available!';
				    }
				} 
				else 
				{
					alert("AJAX Error : Server connection Problem");
				}
			}

}

function reportInvalidDetails(url,userId,obj,page) {
	//obj can be 'phone','email','address','url'
	//alert(userId);

	if(userId=='') {
		//alert("Please enter the Member Id");
		return true;
	}	
	http_request = false;
		if (window.XMLHttpRequest) {
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
			}
		} 
		else if (window.ActiveXObject) { // IE
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}

		if (!http_request) {
			alert('Giving up :( Cannot create an XMLHTTP instance');
			return false;
		}
	

	query = "userId="+userId+"&obj="+obj;
	
	 //url = 'reportInvalidDetails.php';
	
		http_request.open('POST', url, true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send(query);

		//alert(url);
		http_request.onreadystatechange = function ()
		{
			if (http_request.readyState == 4) 
			{
				if (http_request.status == 200) 
				{
					if(page=='supplier') {
						if(obj=='address') {
							className = 'reported';
						}
						else if(obj=='phone') {
							className = 'reported';
						}
						else if(obj=='url') {
							className = 'reported';
						}
					}
					else {
						className = 'reported';
					}		
					if(http_request.responseText=="reported") {
						document.getElementById("reported-"+obj+"-"+userId).style.display='inline';
						document.getElementById("reported-"+obj+"-"+userId).innerHTML='<p class="'+className+'">Reported</p>';
						//document.getElementById("reported-"+obj+"-"+userId).className = className;
					}
					else {
						document.getElementById("reported-"+obj+"-"+userId).style.display='inline';
						document.getElementById("reported-"+obj+"-"+userId).innerHTML='<p class="'+className+'">Not Reported</p>';
				    }
				} 
				else 
				{
					alert("AJAX Error : Server connection Problem");
				}
			}
		}
}


function removeSearchKeyword(key){
	//alert("Removing "+key);
	string = document.getElementById("searchfor").value;
	//alert(string);
	document.getElementById("searchfor").value = string.replace(key,"").replace("  "," ");
}
function displayOrHideCounty(country,docf) {

	
	if(country!='' && country!='Select Country') {			
		if(country==222) {
			countyLength =  docf.memcounty.length
			for(i=0;i<countyLength;i++) {
			  if(docf.memcounty.options[i].value==0) {			  
				   //docf.memcounty.options[i].disabled= true;		  				
				   docf.memcounty.options[0].selected = 'selected';
			   }
			  else {
					//docf.memcounty.options[i].disabled= false;	
			  }
			}		
		}
		else {
			countyLength =  docf.memcounty.length
			for(i=0;i<countyLength;i++) {
			  if(docf.memcounty.options[i].value==0) {
				 // docf.memcounty.options[i].disabled= false;	
				  docf.memcounty.options[i].selected = 'selected';	  				
			  }
			  else {
				  //docf.memcounty.options[i].disabled= true;	
			  }

			}	
		}
	}
	else {
			countyLength =  docf.memcounty.length
			for(i=0;i<countyLength;i++) {	
				if(i==0) {
				   docf.memcounty.options[i].selected = 'selected';	
				}   
			   //docf.memcounty.options[i].disabled= false;	
			}
	 }

}

function displayOrHideCountry(county,docf) {

	
	if(county!='' && county!='Select State') {			
		if(county==0) {
			countryLength =  docf.memcountry.length
			for(i=0;i<countryLength;i++) {
			  if(docf.memcountry.options[i].value==223) {			  
				  //	 docf.memcountry.options[i].disabled= true;		  				
				   docf.memcountry.options[0].selected = 'selected';
			   }
			  else {
					//docf.memcountry.options[i].disabled= false;	
			  }
			}		
		}
		else {
			countryLength =  docf.memcountry.length
			for(i=0;i<countryLength;i++) {
			  if(docf.memcountry.options[i].value==223) {
				  docf.memcountry.options[i].disabled= false;	
				  docf.memcountry.options[i].selected = 'selected';	  				
			  }
			  else {
				  //docf.memcountry.options[i].disabled= true;	
			  }

			}	
		}
	}
	else {
			countyLength =  docf.memcounty.length
			for(i=0;i<countyLength;i++) {		 		  
			   if(i==0) {
				   docf.memcountry.options[i].selected = 'selected';	
				}   
			   //docf.memcountry.options[i].disabled= false;	

			   
			}
	 }

}
function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
	
	var today = new Date();
	today.setTime( today.getTime() );

	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}
// function addToCart(dealId) {
// 	//alert(dealId);
// 	Set_Cookie('dealToAdd',dealId,1,"/",".livewholesale.com");
// 	
// 	window.location.href='http://www.livewholesale.com/cart.php';
// }
function deleteFromCart(dealId) {
	Set_Cookie('dealToDel',dealId,1,"/",".livewholesale.com");
	window.location.href='http://www.livewholesale.com/cart.php';
}
////////////////////////////////////////////////////////////////////////////
//////	JS Functions for Search Validation   //////  VIVEK 19-Dec-08 ///////

function checkIfBlank(form)  // Validation for Category Header Search Box
{
	keyword = form.textfield.value;
	keyword = keyword.replace(/^\s*/, '').replace(/\s*$/, '');
	if(keyword==''){
		alert("Please enter a keyword to search.");
		form.textfield.focus();
		return false;
	}else{
		if(keyword.length<3){
			alert("Please insert a keyword at least 3 characters long.");
			form.textfield.focus();
			return false;
		}
		else if(keyword.length > 200){
			alert("Maximum limit for keywords is 200 characters .");
			form.textfield.focus();
			return false;	
					
		}
		else {
			return true;
		}		
	}	
}
function checkIfBlank2(form)  // Validation for Listing Pages Search Box
{
	keyword = form.searchfor.value;
	keyword = keyword.replace(/^\s*/, '').replace(/\s*$/, '');
	if(keyword==''){
		alert("Please insert a keyword to search!");
		form.searchfor.focus();
		return false;
	}else{
		if(keyword.length<3){
			alert("Please insert a keyword at least 3 characters long.");
			form.searchfor.focus();
			return false;
		}
		else if(keyword.length>200){
			alert("Maximum limit for keywords is 200 characters .");
			form.searchfor.focus();
			return false;	
					
		}
		else {
			return true;
		}		
	}	
}
function checkWithinResults(chk)
{
	if(chk.checked == true){
		createCookie('showwithinresults','Y',7)
	}else{
		createCookie('showwithinresults','N',7)
	}
}
function checkIfGoToBlank(form)  //  Validation for Listing Pages GoTo Paging Button
{
	goto = form.gotopage.value;
	goto = goto.replace(/^\s*/, '').replace(/\s*$/, '');
	if(goto==''){
		alert("Please insert a page number!");
		form.gotopage.focus();
		return false;
	}else{
		if(isNaN(goto)){
			alert("Please insert correct page number!");
			form.gotopage.focus();
			return false;
		}else{
			form.gotopage.value = Math.floor(form.gotopage.value);
			return true;
		}			
	}	
}

function detectKey() { 
	if(event.keyCode==13) { 
		document.topFrmLogin.submit();
	}		
} 

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
function setSortingOption(sortby,url) {
	Set_Cookie('sortcookie',sortby,1,"/",".livewholesale.com");
	window.location.href=url;
}



function showolddeals(show,url) {
	url = "http://www.livewholesale.com/myaccount/manage-deals/main.html";
	Set_Cookie('showolddeals',show,1,"/",".livewholesale.com");
	window.location.href=url;
}

//function filterInput(e) {
   // Get the regular expression to test against for this particular object
   //regAllow = (e)?eval(e.allow):eval(event.srcElement.allow);
   //if (event.keyCode == 13) {
 // Do nothing, i.e. allow.
 //} else {
 // Check for valid input, if invalid, cancel this event.
 //if (!String.fromCharCode(event.keyCode).match(regAllow)) event.returnValue=false;
 //}
// }

function isMemberIdValid(s)
{
    if (isMemberIdCharsInBag (s, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.") == false)
    {
        return false;
    }
    return true;
}

function isMemberIdCharsInBag (s, bag)
{
    var i;
    // Search through string's characters one by one.
    // If character is in bag, append to returnString.
 	bag ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.";
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) return false;
    }
    return true;
}

function add_edit_subscription()
{
	if (document.frm_subscription.subname.value=="")
		{
			alert("Please Provide Subscription Name.");
			document.frm_subscription.subname.focus();
			return false;
		}
	if (document.frm_subscription.subdesc.value=="")
		{
			alert("Please Enter the description.");
			document.frm_subscription.subdesc.focus();
			return false;
		}
	if (document.frm_subscription.noofmonths.value=="")
		{
			alert("Please Provide Subscription Months.");
			document.frm_subscription.noofmonths.focus();
			return false;
		}
	if (document.frm_subscription.subprice.value=="")
		{
			alert("Please Provide Subscription Amount.");
			document.frm_subscription.subprice.focus();
			return false;
		}

}
//alert("HI");
////////////////////////////////////////////////////////////////////////////
