/**
  * Javascript: Fetch the part of the string from right side of the Input string
  * str: Input String
  * n: No. to chracters to fetch from right.
  */
function myRight(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

// Common XAJAX routine to Activate / Deactivate.

function myChangeStatus(theForm, id)
{
	if( confirm('Are you sure you want to change status of this record?')){
		var status=eval('theForm.enumStatus'+id).value;
		return xajax_update_status(id,status);
	}
}



function getHTTPObject(){
	if (window.ActiveXObject) 
		return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) 
		return new XMLHttpRequest();
	else {
		alert("Your browser does not support AJAX.");
		return null;
	}
}


function setOutput(){
if(httpObject.readyState == 4){
alert(httpObject.responseText);
}
}

// Common XAJAX routine to Delete a row.
function delete_row(id)
{
	if( confirm('Are you sure you want to delete this record?'))
		xajax_deleteRow(id);
}

// Other Functions.
Array.prototype.exists = function (x) {
    for (var i = 0; i < this.length; i++) {
    	//alert(this[i].value+' Passed value: '+x);
        if (this[i].value == x) return true;
    }
    return false;
}

function clear_cal_date(field_id)
{
	document.getElementById(field_id).value = ""; // Empty hidden variable value
	document.getElementById('disp_cal_'+field_id).innerHTML = '&nbsp;&nbsp;';
}

function byteCounter(field, countfield, maxlimit,setDiv) {
	if (field.value.length > maxlimit) // if too long...trim it!
	{
		field.value = field.value.substring(0, maxlimit);
		document.getElementById(setDiv).innerHTML = field.value.length+"/"+maxlimit;
		// otherwise, update 'characters left' counter
	}
	else
	{
		countfield.value = maxlimit - field.value.length;
		document.getElementById(setDiv).innerHTML = field.value.length+"/"+maxlimit;
	}
}

function get_element_text(element_id)
{
	var text = "";
	var elem = document.getElementById(element_id);
	if(elem.textContent) {
		text = trim(elem.textContent);
	} else if(elem.innerText) {
		text = trim(elem.innerText);
	}
	return text;
}

// Opens a POP Up window.
function MM_openBrWindow(theURL,winName,features)
{ //v2.0
  	window.open(theURL,winName,features);
}


// Image Swap Javascript
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

// Core function to validate the forms. It reduces the time by just providing the validations for the specified field only. the other not required validations can be skipped.
function validateForm(myForm,cArray,cnt)
{
	
	var i=0;
	var values = "theControl,theMessage,theNumeric,theEmail,theURL,theConfirmPassword,thebadstuff,theinteger,thepercent,thesize,thedecimal,theimage,theempty,themin,themax,thecheckboxlength,thephoneno";
	var leftSide = new Array();
	var rightVals = new Array();
	leftSide = values.split(",");

	var strRepeats = strRepeat(cnt-1);

	for(i=0;i<leftSide.length;i++){
		if(cArray[leftSide[i]]){
			rightVals[i] = (cArray[leftSide[i]]);
		}else{
			rightVals[i] = strRepeats;
		}
		leftSide[i]=rightVals[i];
	}

	if(!theValidator(myForm,leftSide[0],leftSide[1],leftSide[2],leftSide[3],leftSide[4],leftSide[5],leftSide[6],leftSide[7],leftSide[8],leftSide[9],leftSide[10],leftSide[11],leftSide[12],leftSide[13],leftSide[14],leftSide[15],leftSide[16]))
		return false;

	return true;
}



// Validates if the element is present in the document or not ?
function isDefined(variable,type_evt)
{
	try
	{
		if(type_evt==1)
		{
			if (variable == null)
			{
				return false;
			}

			if ("undefined" == typeof(variable) )
			{
				return false;
			}
			return true;
		}
		else
			return (typeof(window[variable]) == "undefined")?  false: true;
	}
	catch(e)
	{
		//alert('here.');
	}
}

// Repeats the particular string to the specified number of times.
function strRepeat(cnt)
{
	var retStr = "";
	for(i=0;i<cnt;i++)
		retStr=retStr+",";
	return retStr;
}



function validate_FckEditor(instanceName)
{
	var oEditor = FCKeditorAPI.GetInstance(instanceName);
	var str =oEditor.EditorDocument.body.innerHTML;
	str = str.replace("&nbsp;", "");
     	str = str.replace("<BR>", "");
     	str = str.replace("<BR/>", "");
     	str = str.replace("<P>", "");
     	str = str.replace("</P>", "");
     	str = str.replace("<br>", "");
     	str = str.replace("<br/>", "");
     	str = str.replace("<p>", "");
     	str = str.replace("</p>", "");
     	if(str == "")
     		return false;
     	else
     		return true;
}
function addContent_FckEditor(instanceName,inputElement){
	var oEditor = FCKeditorAPI.GetInstance(instanceName);
	var str =oEditor.EditorDocument.body.innerHTML;
	oEditor.EditorDocument.body.innerHTML=str+inputElement.value;
}
function clear_FckEditorContent(instanceName){
	var oEditor = FCKeditorAPI.GetInstance(instanceName);	
	oEditor.EditorDocument.body.innerHTML="";
}
function chkOptionsAll(act,theFormID,ccheckbox,ccount)
{
	theForm=document.getElementById(theFormID);
	
	if(chkOptionsCount(theForm,ccheckbox))
	{
		if(act=='A')
		{
			if(confirm('Are you sure you want to activate these records?'));
			else
				return false;
		}
		else if(act=='D'){
			if(confirm('Are you sure you want to deactivate these records?'));
			else
				return false;
		}else if(act=='Delete')
		{
			if(confirm('Are you sure you want to delete these records?'));
			else
				return false;
		}
		
		var strChkId='';chkID = '';
		var i;
		for(i=0; i<theForm.elements.length; i++)
		{
		
			//alert(theForm.elements[i].name);
			if(theForm.elements[i].name==''+ccheckbox+'[]' && theForm.elements[i].checked)
			{
			//	alert(theForm.elements[i].value);
			//	alert(theForm.elements[i].name);
				if(chkID == ""){
						var chkID = theForm.elements[i].value;

				}else{
						chkID = chkID +","+theForm.elements[i].value;
				}
				theForm.elements[i].checked=false;
			}
			
		}
		if(document.getElementById('checkAll')){
			document.getElementById('checkAll').checked=false;
		}
	   // document.getElementById('checkStatus').value = "";
		if(act == 'Delete')
		{
			
			
					return xajax_deleteRow(chkID);
			

			//call_page_after_delete(2,"/admin/promotions.php");
		}else if(act == 'A'){
			
					return xajax_update_status(chkID,'D');
				
						
		}else if(act == 'D'){
				
					return xajax_update_status(chkID,'A');
				

						
		}
		
	}
	else
	{
		return false;
	}
}
function chkOptions_all(act,theFormID,ccheckbox,ccount)
{
	theForm=document.getElementById(theFormID);
	
	if(chkOptions(theForm,ccheckbox))
	{
		if(act=='A')
		{
			if(confirm('Are you sure you want to activate these records?'));
			else
				return false;
		}
		else if(act=='D'){
			if(confirm('Are you sure you want to deactivate these records?'));
			else
				return false;
		}else if(act=='Delete')
		{
			if(confirm('Are you sure you want to delete these records?'));
			else
				return false;
		}
		
		var strChkId='';chkID = '';
		var i;
		for(i=0; i<theForm.elements.length; i++)
		{
		
			//alert(theForm.elements[i].name);
			if(theForm.elements[i].name==''+ccheckbox+'[]' && theForm.elements[i].checked)
			{
			//	alert(theForm.elements[i].value);
			//	alert(theForm.elements[i].name);
				if(chkID == ""){
						var chkID = theForm.elements[i].value;

				}else{
						chkID = chkID +","+theForm.elements[i].value;
				}
				theForm.elements[i].checked=false;
			}
			
		}
		if(document.getElementById('checkAll')){
			document.getElementById('checkAll').checked=false;
		}
	   // document.getElementById('checkStatus').value = "";
		if(act == 'Delete')
		{
			
			
					return xajax_deleteRow(chkID);
			

			//call_page_after_delete(2,"/admin/promotions.php");
		}else if(act == 'A'){
			
					return xajax_update_status(chkID,'D');
				
						
		}else if(act == 'D'){
				
					return xajax_update_status(chkID,'A');
				

						
		}
		
	}
	else
	{
		return false;
	}
}
function chkOptions(theForm,ccheckbox)
{
	var resVal='';
	//alert(ccheckbox);
	for(i=0; i<theForm.elements.length; i++)
	{
		
			//alert(theForm.elements[i].name==''+ccheckbox+'[]');
			if(theForm.elements[i].name==''+ccheckbox+'[]' && theForm.elements[i].checked)
			{
				
				if(resVal == "")
					var resVal=theForm.elements[i].value;
				else
					resVal = resVal + "," + theForm.elements[i].value;
			}
		
	}
	if(resVal=='')
	{
		alert('Please select a record.');
		return false;
	}
	else
	{
		//alert(resVal);
		return true;
	}
}
function chkOptionsCount(theForm,ccheckbox)
{
	var cnt;
	cnt=0;
	var count;
	count=0;
	var remainingCnt;
	remainingCnt=0;
	//alert(ccheckbox);
	for(i=0; i<theForm.elements.length; i++)
	{
		
		//alert(theForm.elements[i].name==''+ccheckbox+'[]');
		if(theForm.elements[i].name==''+ccheckbox+'[]'){
				count++;
				if(theForm.elements[i].checked){
					cnt++;
				}
		}
			
			
		
		
	}
	remainingCnt=parseInt(count)-parseInt(cnt);

	//alert(count);
	//alert(cnt);
	//alert(remainingCnt);

	if(remainingCnt<5)
	{
		alert('Minimum order value should be $10.00.');
		return false;
	}
	else
	{
		//alert(resVal);
		return true;
	}
}



function selChecks(numOfChecks,intCheckBox){

	var chkLen = document.getElementById(numOfChecks).value;

	//alert(chkLen);
	
	if(document.getElementById('checkAll').checked==true){
		for (i=1;i<=chkLen;i++){
			
			var obj=intCheckBox+i;	
			
			document.getElementById(obj).checked=true;			
			
		}
	}else{
		for (i=1;i<=chkLen;i++){
			
			var obj=intCheckBox+i;			
			document.getElementById(obj).checked=false;			
			
		}
	}
}




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 resetForm(theForm){
	document.getElementById(theForm).reset();
}
function sortPages(sortOrder,sortBy){
	
	document.frmSort.sortBy.value=sortBy;
	document.frmSort.sortOrder.value=sortOrder;
	document.frmSort.submit();
}
function topFormSubmit(file,tdId){
	
	//document.getElementById(tdId).className='mainlinkselected';	
	currentTdId=tdId;	

	document.topMenuForm.currentTdId.value=tdId;
	//alert(file);
	document.topMenuForm.action=file;	
	document.topMenuForm.submit();
}
function unselectListbox(list) //list -> listbox object.
{
    var len = list.length;
   
    for(i=0; i<len; i++)
    {               
        list.options.item(i).selected = false;
    }
}

/*----------------------------Multiple selection in list box on just single click on-off------------------------------------*/

//Call method in hmtl 
/*<select onchange="FillListValues(this);" onmousedown="GetCurrentListValues(this);" multiple="" id="select" name="Christmas_List" size="11">
    <option>Widescreen TV</option>
    <option>DVD Recorder</option>
    <option>Digital Camcorder</option>
    <option>Digital Camera</option>
    <option>Satsuma</option>
    <option>Hi Fi System</option>
    <option>Speakers</option>
    <option>Electric Guitar</option>
    <option>Portastudio</option>
    <option>Sports Car</option>
    <option>Tasty Wench</option>
  </select>
*/

var arrOldValues;

function FillListValues(CONTROL){
var arrNewValues;
var intNewPos;
var strTemp = GetSelectValues(CONTROL);
arrNewValues = strTemp.split(",");
for(var i=0;i<arrNewValues.length-1;i++){
if(arrNewValues[i]==1){
intNewPos = i;
}
}

for(var i=0;i<arrOldValues.length-1;i++){
if(arrOldValues[i]==1 && i != intNewPos){
CONTROL.options[i].selected= true;
}
else if(arrOldValues[i]==0 && i != intNewPos){
CONTROL.options[i].selected= false;
}

if(arrOldValues[intNewPos]== 1){
CONTROL.options[intNewPos].selected = false;
}
else{
CONTROL.options[intNewPos].selected = true;
}
}
}


function GetSelectValues(CONTROL){
var strTemp = "";
for(var i = 0;i < CONTROL.length;i++){
if(CONTROL.options[i].selected == true){
strTemp += "1,";
}
else{
strTemp += "0,";
}
}
return strTemp;
}

function GetCurrentListValues(CONTROL){
var strValues = "";
strValues = GetSelectValues(CONTROL);
arrOldValues = strValues.split(",")
}

function validateNumber(control, number, msg) 
{
if(isNaN(number))
	{
		alert(msg);
		control.focus(); 
	}
}

/*----------------------------Multiple selection in list box on just single click on-off------------------------------------*/
