// JavaScript Document

//Global Variables

var GcatVSts=0;
var GIsSet=0;
var ExMth=0;

// Call function after page load






function trim(stringToTrim) {//trim function
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

/*These functions are written in order to replace the javascript alert*/
function write_alert(){//to prepare the alert frame
	var msg_inner = '<div id="alert_content"></div>';
	msg_inner += '<div><a href="javascript:void(remove_alert());">Ok</a></div>';
	var msg_content = '<div id="alert_div" style="display:none;"><div id="msg_inner">'+msg_inner+'</div></div>';
	document.write(msg_content);
}
function write_confirm(){//to prepare the confirm frame
	var msg_inner = '<div id="confirm_content"></div>';
	msg_inner += '<div><a href="javascript:void(proceed_confirm());">Ok</a>&nbsp;<a href="javascript:void(remove_confirm());">Cancel</a></div>';
	var msg_content = '<div id="confirm_div" style="display:none;"><div id="msg_inner">'+msg_inner+'</div ></div>';
	document.write(msg_content);
}

function custom_alert(msg){//to display the alert
	document.getElementById("alert_content").innerHTML = msg;
	document.getElementById("alert_div").style.display = 'block';
	//setTimeout('remove_alert()',5000);
}
var confirm_callback_function = "";
var callback_arr = new Array();
function custom_confirm(msg){//to display the confirm
		document.getElementById("confirm_content").innerHTML = msg;
		document.getElementById("confirm_div").style.display = 'block';
}
function remove_alert(){//to remove the alert
	document.getElementById("alert_content").innerHTML = "";
	document.getElementById("alert_div").style.display = 'none';
}
function remove_confirm(){//to remove the confirm
	document.getElementById("confirm_content").innerHTML = "";
	document.getElementById("confirm_div").style.display = 'none';
	confirm_callback_function(false);
}
function proceed_confirm(){//to proceed the confirm
	document.getElementById("confirm_content").innerHTML = "";
	document.getElementById("confirm_div").style.display = 'none';
	confirm_callback_function(true);
}
/*alert funtions ends here*/

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function validateEmail(thisObjVal) {
		var str	= thisObjVal.value;
		if(str==''){
			return false;	
		}
		var at="@"
		var dot=".";
		var errorFlg	= 0;
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID");
		   errorFlg	= 1;
		}else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID");
		   errorFlg	= 1;
		}else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID");
			errorFlg	= 1;
		}else if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID");
			errorFlg	= 1;
		}else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID");
			errorFlg	= 1;
		}else if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID");
			errorFlg	= 1;
		}else if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID");
			errorFlg	= 1;
		}
		if(errorFlg==1){
			thisObjVal.value	='';
			thisObjVal.focus();
		    return false
		}
 		return true					
	}
	
	
	
/* check all start*/

	
/*function CheckAll(chk)
{
	var ml = document.frmdelete;
	var len = chk.length;	

	for (var i = 0; i < len; i++) 
	{	
		var e = ml.elements[i];	
		
		e.checked = true;
			
		
	}	
	ml.delid.checked = true;
}


function UnCheckAll(chk)
{
	var ml = document.frmdelete;
	var len = chk.length;
	for (var i = 0; i < len; i++) {
		var e = ml.elements[i];
		e.checked = false;
	}
	ml.delid.checked = false;
}

function CheckAll_new(chk)
{
	
	var ml = document.frmdelete;
	var len = chk.length;	

	for (var i = 1; i <= len; i++) 
	{	
		var e = ml.elements[i];	
		
		e.checked = true;
			
		
	}	
	ml.delid.checked = true;
}
*/



/* function to select all items in a select box  */

function mul_selectAll(selectBox,selectAll) {

	// have we been passed an ID
	if (typeof selectBox == "string") {
		selectBox = document.getElementById(selectBox);
	}
	// is the select box a multiple select box?

	if (selectBox.type == "select-multiple") {
			
		for (var i = 0; i < selectBox.options.length; i++) {
			selectBox.options[i].selected = selectAll;
		}
	}
}
	
function chksearch()
{
		if(trim(document.getElementById('keywords').value) == "")
		{
			custom_alert("Please enter a keyword to search");	
			document.getElementById('keywords').value="";
			document.getElementById('keywords').focus();
			return false;
		}
		return true;
}

function  hide_for_value(div,ctrl,showval){
	document.getElementById(div).style.display="none";
	var vals=		document.getElementById(ctrl).value;


	if(vals==showval){
	document.getElementById(div).style.display=document.getElementById(ctrl).style.display;

	
	}else{
			document.getElementById(div).style.display="none";
	}
		
}
function setCount(parentId,counterId,maxCount)
{
	document.getElementById(counterId).value=(parseInt(maxCount)-parseInt(document.getElementById(parentId).value.length));
	if(parseInt(document.getElementById(parentId).value.length)>maxCount)
	{
		document.getElementById(parentId).value=document.getElementById(parentId).value.substring(0,maxCount);
		document.getElementById(counterId).value="0";
	}
}

var actLmtAry=new Array(); 

function validateCheck()
{
	var maxmod=document.getElementById("maxmod").value;
	var maxact=document.getElementById("maxact").value;
	var i=0;
	var j=0;
	var cFlg=0;
	for(i=0;i<parseInt(maxmod);i++)
	{
		cFlg=0;
		for(j=0;j<parseInt(actLmtAry[i]);j++)
		{
			if(document.getElementById(String(i)+String(j)).checked==true)
			{
				cFlg=1;
				break;
			}
		}
		if(cFlg==0)
		{
			var answer = confirm("No functinality is not checked for at least one module. Do you want to continue?");	
			if(answer)
				return true;
			else	
				return false;
		}
	}
}


function generateCoupon(coupnCountId,charCnt,isMxd,containerId){
	var i	=	0;
	var couponAry	=	new Array(); 
	var isExtCnt	=	0;
	var couponContainer	=	document.getElementById(containerId);
	var coupnCount	=	document.getElementById(coupnCountId).value;
	
	var numberOfOptions = couponContainer.options.length  
	for (i=0; i<numberOfOptions; i++) {  
		couponContainer.remove(0)  
	}  
	
	if(coupnCount<50){
		couponContainer.size	=	coupnCount;
	}else{
		couponContainer.size	=	50;
	}
	
	for(i=0;i<coupnCount;i++){
		
		couponTemp	=	randomString(charCnt,isMxd);

		if(isCouponValid(couponTemp)){
			var opt = document.createElement("option");
			
			couponAry[i]	=	couponTemp;
			isExtCnt	=	0;
			
			couponContainer.options.add(opt);
    		opt.text = couponTemp;
    		opt.value = couponTemp;
		}else{
			isExtCnt++;
			if(isExtCnt>1000){
				break;
			}
			i--;
		}

	}
	
	if(couponContainer.size>0){
		document.getElementById("couponList").style.display="table-row";
	}
	
	
}


function isCouponValid(couponVal){	
	var xmlHttp;	
	xmlHttp=GetXmlHttpObject();	
	if (xmlHttp==null){	  alert ("Your browser does not support AJAX!");	  return;	}	
	var url="Ajax_pages/isCouponExists.php?couponCode="+couponVal;	
	xmlHttp.open("GET",url,false);	
	xmlHttp.send();		
	var rData=xmlHttp.responseText;	
	if(rData=="1"){
		return false;	
	}else{		
		return true;	
	}
}

function randomString(string_length,isMxd) {
	var chars;
	if(isMxd==1){
		chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	}else{
		chars = "0123456789";
	}
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}


function removeOptions(selectboxId,countId){
	var i;
	var selectbox	=	document.getElementById(selectboxId);
	for(i=selectbox.options.length-1;i>=0;i--){
		if(selectbox.options[i].selected)
		selectbox.remove(i);
	}
	document.getElementById(countId).value	=	selectbox.length;
	
	if(selectbox.length<50){
		selectbox.size	=	selectbox.length;
	}else{
		selectbox.size	=	50;
	}
}

function shipInclude(){
	var stsId	=	document.getElementById("shp_include").value;
	if(stsId==0){
		document.getElementById("fxdShpingCst").style.display	=	"table-row";	
	}else{
		document.getElementById("fxdShpingCst").style.display	=	"none";
	}
}

function selectAllCpn(selectboxId){
	var i;
	var selectbox	=	document.getElementById(selectboxId);
	for(i=selectbox.options.length-1;i>=0;i--){
		selectbox.options[i].selected	=	true;
	}

}
function validateCouponSbt(cartId){
	var tocketValue	=	document.getElementById("coupon").value;
	var ErrId	=	document.getElementById("couponErrMsg");
	if(tocketValue==""){
		ErrId.innerHTML	=	"Please enter Coupon Code";
		return false;	
	}else{
		if(!isValidCoupon(tocketValue,cartId)){
			ErrId.innerHTML	=	"invalid Coupon Code";
			return false;	
		}
	}
	return true;	
}

 

function isValidCoupon(couponCode,cartId)
{
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
	  alert ("Your browser does not support AJAX!");
	  return;
	}
	var url="ajax/checkCoupon.php?couponCode="+couponCode+"&cartId="+cartId;
	xmlHttp.open("GET",url,false);
	xmlHttp.send();
	
	var rData=xmlHttp.responseText;

	if(rData==1){
		return true;	
	}else{
		return false;	
	}
} 

