var main_dir = '/cls/';
var home_path = 'http://'+location.hostname+main_dir;
var flowplayer_path = 'http://'+location.hostname+main_dir+'flowplayer/';


function writeScriptError(s) { 
         alert(s);
}

function placeFocus() {
	if (document.forms.length > 0) {
		valueChanged = false;
		var field = document.forms[0];
		for (i = 0; i < field.length; i++) {
			if ((field.elements[i].type == "text") || 
			    (field.elements[i].type == "textarea") || 
			    (field.elements[i].type == "checkbox") || 
			    (field.elements[i].type == "select-one")) {
				if (field.elements[i].disabled == false) {
					document.forms[0].elements[i].focus();
					break;
				}
         	}
      	}
   	} else {
		return false;
	}
}

function disableEnterKey(key) {
	if (key == 13) { 		
		return false;
	} else { 
		return true;
	}
}

function disableBackspaceKey(key) {
	if (key == 8 || key == 13) { 		
		return false;
	} else { 
		return true;
	}
}

function disableCopyPasteKey(key) {
	if (key == 22) { 		
		return false;
	} else { 
		return true;
	}
}

function trim(strVar) {
	while(strVar.charAt(strVar.length-1)==' ')
		strVar=strVar.substring(0,strVar.length-1);

	while(strVar.charAt(0)==' ')
		strVar=strVar.substring(1,strVar.length);

	return strVar;
}

function isNumber(toCheck){
	var reNumber = /^\d+$/;
	return reNumber.test(toCheck);
}

function isNumberEntry(form,toCheck,toCheckName) {
	if (!isNumber(toCheck.value)) {
		writeScriptError("Please enter a valid numeric input (numbers only) in "+toCheckName+".");
		eval("window.document." + form.name + "." + toCheck.name + ".focus()");
		return false;
    }
	return true;
}

function isAlpha(toCheck){
	var reAlpha = /^[a-zA-Z]+$/;
	return reAlpha.test(toCheck);
}

function isAlphaEntry(form,toCheck,toCheckName) {
	if (!isAlpha(toCheck.value)) {
		writeScriptError("Please enter a valid alpha input (alphabets only) in "+toCheckName+".");
		eval("window.document." + form.name + "." + toCheck.name + ".focus()");
		return false;
    }
	return true;
}

function isCurrency(toCheck){
	//var reCurr = /^\s*\d+\.\d{2}\s*$/;
	var reCurr = /^\d*(\.\d{0,2})?$/;
	return reCurr.test(toCheck);
}

function isCurrencyEntry(form,toCheck,toCheckName) {
	if (!isCurrency(toCheck.value)) {
		writeScriptError("Please enter a valid currency input in "+toCheckName+".");
		eval("window.document." + form.name + "." + toCheck.name + ".focus()");
		return false;
    }
	return true;
}

function isBlank(toCheck) {
	var reBlank = /^\s*$/;
	return reBlank.test(toCheck);
}

function isBlankEntry(form,toCheck,toCheckName) {
	if (isBlank(toCheck.value)) {
		writeScriptError("Please enter " + toCheckName + ".");
		eval("window.document." + form.name + "." + toCheck.name + ".focus()");
		return true;
    }
	return false;
}

function isBlankAndZeroEntry(form,toCheck,toCheckName) {
	if (isBlank(toCheck.value)) {
		writeScriptError("Please enter " + toCheckName + ".");
		eval("window.document." + form.name + "." + toCheck.name + ".focus()");
		return true;
    } 
    	
	if (isBlank(trimLeadingZeros(toCheck.value))) {
		writeScriptError("Zeroes are not allowed.");
		eval("window.document." + form.name + "." + toCheck.name + ".focus()");
		return true;
    }
	return false;
}

function isComboSelected(form,toCheck,toCheckName) {
	if (eval("window.document." + form.name + "." + toCheck.name + ".selectedIndex") == 0) {
		writeScriptError("Please select " + toCheckName + ".");
		eval("window.document." + form.name + "." + toCheck.name + ".focus()");
		return false;
    }
	return true;
}

function isOnlyComboSelected(form,toCheck) {
	if (eval("window.document." + form.name + "." + toCheck.name + ".selectedIndex") == 0) {
		return false;
    }
	return true;
}

function isDate(toCheck) {
	var reDate = /^(((0[1-9]|[12][0-9]|3[01])([-./])(0[13578]|10|12)([-./])(\d{4}))|(([0][1-9]|[12][0-9]|30)([-./])(0[469]|11)([-./])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([-./])(0[2])([-./])(\d{4}))|((29)(\.|-|\/)(0[2])([-./])([02468][048]00))|((29)([-./])(0[2])([-./])([13579][26]00))|((29)([-./])(0[2])([-./])([0-9][0-9][0][48]))|((29)([-./])(0[2])([-./])([0-9][0-9][2468][048]))|((29)([-./])(0[2])([-./])([0-9][0-9][13579][26])))$/;
	return reDate.test(toCheck);
}

function isDateEntry(form,toCheck,toCheckName) {
	var reDate = /^(((0[1-9]|[12][0-9]|3[01])([-./])(0[13578]|10|12)([-./])(\d{4}))|(([0][1-9]|[12][0-9]|30)([-./])(0[469]|11)([-./])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([-./])(0[2])([-./])(\d{4}))|((29)(\.|-|\/)(0[2])([-./])([02468][048]00))|((29)([-./])(0[2])([-./])([13579][26]00))|((29)([-./])(0[2])([-./])([0-9][0-9][0][48]))|((29)([-./])(0[2])([-./])([0-9][0-9][2468][048]))|((29)([-./])(0[2])([-./])([0-9][0-9][13579][26])))$/;
	if (toCheck.value != '') {
		if (!reDate.test(toCheck.value)) {
			writeScriptError(toCheckName + " is not a valid Calendar Date.");
			eval("window.document." + form.name + "." + toCheck.name + ".focus()");
			return false;
	    }
	}
	return true;
}

function isEmail(toCheck) {
	var reEmail = /^[_#a-zA-Z0-9-]+(\.[_#a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,3})$/;
	return reEmail.test(toCheck);
}

function isEmailEntry(form,toCheck,toCheckName) {
	if (!isEmail(toCheck.value)) {
		writeScriptError("Not a valid email.");
		eval("window.document." + form.name + "." + toCheck.name + ".focus()");
		return false;
	}
	
	return true;
}

function isUserId(toCheck){
	var reAlpha = /^[a-zA-Z0-9._%-]+$/;
	return reAlpha.test(toCheck);
}

function isUserIdEntry(form,toCheck,toCheckName) {
	if (!isUserId(toCheck.value)) {
		writeScriptError("Please enter a valid alpha-numeric (alphabets and numbers only) in "+toCheckName+".");
		eval("window.document." + form.name + "." + toCheck.name + ".focus()");
		return false;
    }
	return true;
}

function isNvl(obj) {
	var strVal = obj.value;
	if(isBlank(strVal)) {
 		return 0;
	} else {
		if (strVal.indexOf(".") >= 0) {
			for(var i=0;i<strVal.length;i++) {
				strVal = strVal.replace(",","");
			}
			return parseFloat(strVal);
		} else {
 			return Number(strVal);
		}
	}
}

function trimLeadingZeros(strVar) {
	var i = 0;
	var previousValue = strVar;

	while(strVar.charAt(i)=='0' || strVar.charAt(i)=='.' || strVar.charAt(i)==',' ) {
		strVar=strVar.substring(1,strVar.length);
		i = 0;
	}

	return strVar;
}

function filterCommas(str) {
	re = /^\$|,/g;
	return str.replace(re, "");
}

function convertNumberToCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');

	if(isNaN(num))
		num = "0";

	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();

	if(cents<10)
		cents = "0" + cents;

	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+

	num.substring(num.length-(4*i+3));

	return (((sign)?'':'-') + num + '.' + cents);
}

function limitTextArea(field, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}
}

function autoNextField(event) {
	if (event.keyCode==13) {
		event.keyCode=9;
	}
}

function allowNumeric(event) {
	if (event.keyCode < 48 || event.keyCode > 57) 
		event.returnValue = false;
}

function allowAlpha(event) {
	if ((event.keyCode >= 65 && event.keyCode <= 90) || (event.keyCode >= 97 && event.keyCode <= 122)) 
		event.returnValue = true;
	else
		event.returnValue = false;
}

function printContent(id){
	str=document.getElementById(id).innerHTML
	newwin=window.open('','printwin','scrollbars=yes,toolbars=yes')
	newwin.document.write('<HTML>\n<HEAD>\n')
	newwin.document.write('<TITLE>Print Page</TITLE>\n')
	newwin.document.write('<link rel="stylesheet" href="../../style_print.css" type="text/css" media="screen" />')
	newwin.document.write('<script>\n')
	newwin.document.write('function chkstate(){\n')
	newwin.document.write('if(document.readyState=="complete"){\n')
	newwin.document.write('window.close()\n')
	newwin.document.write('}\n')
	newwin.document.write('else{\n')
	newwin.document.write('setTimeout("chkstate()",2000)\n')
	newwin.document.write('}\n')
	newwin.document.write('}\n')
	newwin.document.write('function print_win(){\n')
	newwin.document.write('window.print();\n')
	newwin.document.write('chkstate();\n')
	newwin.document.write('}\n')
	newwin.document.write('<\/script>\n')
	newwin.document.write('</HEAD>\n')
	newwin.document.write('<BODY onload="print_win()">\n')
	newwin.document.write(str)
	newwin.document.write('</BODY>\n')
	newwin.document.write('</HTML>\n')
	newwin.document.close()
}

function showHideDetails(elementid,imgid){
	if (document.getElementById(elementid).style.display == 'none'){
		document.getElementById(elementid).style.display = '';
		document.getElementById(imgid).src = '../../images/expand.gif';
	} else {
		document.getElementById(elementid).style.display = 'none';
		document.getElementById(imgid).src = '../../images/collapse.gif';
	}
}

function showHideAdvanceSearch(elementid,imgid){
	if (document.getElementById(elementid).style.display == 'none'){
		document.getElementById(elementid).style.display = '';
		document.getElementById(imgid).src = '../../images/collapse_minus.gif';
	} else {
		document.getElementById(elementid).style.display = 'none';
		document.getElementById(imgid).src = '../../images/expand_plus.gif';
	}
}

function image_upload(product_id,server_path,random_image_id) {
	if (product_id == null) {
		product_id = '';
	}
	var width = 650;
    var height = 650;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" + height + ",location=no,directories=no,scrollbars=yes,toolbars=no,maximize=yes,resize=yes,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
    var url = server_path+"gallery_management.php?pid="+product_id+"&rmid="+random_image_id;
	window.open(url,'Gallery_Management',windowFeatures);
}

function importScript(url){
    var tag = document.createElement("script");
    tag.type="text/javascript";
    tag.src = url;
    document.body.appendChild(tag);
}

function adjustHeightOfContent() {
	content_height = document.getElementById("content_main_wrapper").offsetHeight;

	if (content_height < 375) {
		document.getElementById("content_main_wrapper").style.height = '375px';
	}
}