// JavaScript Document
var ctrl, msgstring, msg, ctrlfocus, msgstring1, msg1
function removechar(inputString, removeChar) 

	{	var returnString = inputString;
		if (removeChar.length)
		{
		  while(''+returnString.charAt(0)==removeChar)
			{
			  returnString=returnString.substring(1,returnString.length);
			}
			while(''+returnString.charAt(returnString.length-1)==removeChar)
		  {
			returnString=returnString.substring(0,returnString.length-1);
		  }
		}
		return returnString;
	}	
function go2validateemail(str)
{
	var supported = 0;
  if (window.RegExp) {
	var tempStr = "a";
	var tempReg = new RegExp(tempStr);
	if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	  return (!r1.test(str) && r2.test(str));

}
function checkemail(str)
{
		//var str=document.frm_register.email.value;
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		if (filter.test(str))
			testresults=true;
		else{
			//alert("Please insert a valid E-mail Address!");
			testresults=false;
		}
		return (testresults);
}
function validdate(str)
{
  var regexp = new RegExp(/\d{4}\-\d{2}\-\d{2}/);
  return regexp.test(str);
}
function trim(tmp)
{
	//tmp = "      this is test     ";
	pat = /^\s+/;
	tmp = tmp.replace(pat, "");
	pat = /\s+$/;
	tmp = tmp.replace(pat, "");
	//alert(":" + tmp + ":");
	return tmp;
}

function isdecimal(text)
{
	var isValid = true;
    var validChars = "1234567890.";
    var charIndex;
	   for (charIndex = 0; charIndex < text.length;charIndex++)
	   {
		  if (validChars.indexOf(text.charAt(charIndex).toLowerCase()) < 0)
		  {
			 isValid = false;
			 break;
		  }		  
	   }
	   if(text.lastIndexOf('.')!=text.indexOf('.'))
 	   {
		  	 isValid = false;
  		}
	   return isValid;
}
function removedecimal(val)
{
	var result = 0;
	//alert(isNaN(val));
	if(isNaN(val)==true || val=="")
	{
		
		result = 0;
	}
	else
	{
		result=parseInt(val);
		
	}
	return result;
}

function intfloat()
{	
	if((event.keyCode>=48 && event.keyCode<=57) || event.keyCode==46 || event.keyCode==13) 
	{}
	else
	{	event.keyCode=0;	}
}
function comparedate(fromdate,todate)
{
		
		var pos1=fromdate.indexOf(dtCh);
		var daysInMonth = DaysArray(12);
		var pos2=fromdate.indexOf(dtCh,pos1+1);
		var strMonth=fromdate.substring(0,pos1);
		var strDay=fromdate.substring(pos1+1,pos2);
		var strYear=fromdate.substring(pos2+1);
		
		
		var pos1=todate.indexOf(dtCh);
		var daysInMonth = DaysArray(12);
		var pos2=todate.indexOf(dtCh,pos1+1);
		var strMonth2=todate.substring(0,pos1);
		var strDay2=todate.substring(pos1+1,pos2);
		var strYear2=todate.substring(pos2+1);
		
if((strYear>strYear2) || (strYear==strYear2 && strMonth>strMonth2) || (strYear==strYear2 && strMonth==strMonth2 && strDay>strDay2))
{
	return false;
}
		return true;
		
}
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_showHideLayers() 
{ //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}


function multiEmail(email_field) 
{
var email = email_field.split(',');
for (var i = 0; i < email.length; i++) {
if (!validateEmail(email[i], 1, 0)) {
alert('one or more email addresses entered is invalid');
return false;
}
}
return true;
} 

function formatNumber(myNum, numOfDec) 
{ 
var decimal = 1 
for(i=1; i<=numOfDec;i++) 
decimal = decimal *10 

var myFormattedNum = (Math.round(myNum * decimal)/decimal).toFixed(numOfDec) 
return(myFormattedNum) 
} 
function check_all()
{
for( x=0; x<document.frm.elements.length; x++ ) {
	var e = document.frm.elements[x];
	if(e.name == "did" && e.disabled==false)
		e.checked = document.frm.check.checked;
	}
}

function tick_untick()
{
var item_checked = 0;
var item_total = 0;
for( x=0; x<document.frm.elements.length; x++ ) {
	var e = document.frm.elements[x];
	if ( e.name == "did" ) {
		item_total++;
		if ( e.checked == true ) item_checked++;
		}
	document.frm.check.checked = (item_checked == item_total);
	}
}

function check_delete(frm,redirect_to)
{	
var item_checked = 0;
var ids = "";
for( x=0; x<document.frm.elements.length; x++ ) {
	var e = document.frm.elements[x];
	if ( e.name == "did" && e.checked == true ) 
	{
		ids = ids + e.value + ",";
		item_checked++;
	}
	}
if ( item_checked == 0 )
	alert("You must select atleast one item to delete.");
else {
	if ( confirm("Are you sure to delete selected item(s).") ) {
		frm.mode.value = 'delete';
		frm.ids.value = ids;
		frm.action = redirect_to;
		frm.submit();
		}
	}
}
function check_batch_update(frm,redirect_to)
{
	if(checkEmptyString("document.frm.batchvalue,Batch value"))
	{
		alert(msgstring);
		eval(ctrlfocus);
		return false;
	}
	var item_checked = 0;
	var ids = "";
	for( x=0; x<document.frm.elements.length; x++ ) {
		var e = document.frm.elements[x];
		if ( e.name == "did" && e.checked == true ) 
		{
			ids = ids + e.value + ",";
			item_checked++;
		}
	}
	if ( item_checked == 0 )
		alert("You must select atleast one item to update batch list.");
	else {
		if ( confirm("Are you sure want to update batch for selected product(s).") ) {
			frm.mode.value = 'delete';
			frm.ids.value = ids;
			frm.action = redirect_to;
			frm.submit();
			}
		}
}

function check_update(frm,redirect_to, ctrlname)
{
var item_checked = 0;
var ids1 = "";
var ids2 = "";
for( x=0; x<document.frm.elements.length; x++ ) {
	var e = document.frm.elements[x];
	
	if ( e.name.substr(e.name,3) == ctrlname) 
	{
		if(e.checked == false)
			ids2 = ids2 + e.value + ",";
		else
			ids1 = ids1 + e.value + ",";
	}
	}
	if (frm.checkchange.value == 1) {
		frm.mode.value = 'update';
		frm.ids.value = ids1;
		frm.list.value = ids2;
		frm.action = redirect_to;
		frm.submit();
		}
}

var marked_row = new Array;

function setColor(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 3.3 ... Opera changes colors set via HTML to rgb(r,g,b) format so fix it
    if (currentColor.indexOf("rgb") >= 0) 
    {
        var rgbStr = currentColor.slice(currentColor.indexOf('(') + 1,
                                     currentColor.indexOf(')'));
        var rgbValues = rgbStr.split(",");
        currentColor = "#";
        var hexChars = "0123456789ABCDEF";
        for (var i = 0; i < 3; i++)
        {
            var v = rgbValues[i].valueOf();
            currentColor += hexChars.charAt(v/16) + hexChars.charAt(v%16);
        }
    }

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
}


function isEmail(str) 
{
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
	var tempStr = "a";
	var tempReg = new RegExp(tempStr);
	if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	  return (!r1.test(str) && r2.test(str));
}

function ajaxinclude(url, containerid) {
	var page_request = false
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e){
		try{
			page_request = new ActiveXObject("Microsoft.XMLHTTP")
		}
		catch (e){}
		}
	}
	else
	return false
	page_request.open('GET', url, false) //get page synchronously 
	page_request.send(null)
	writecontent(page_request, containerid)
}

function writecontent(page_request, containerid){
if (window.location.href.indexOf("http")==-1 || page_request.status==200)
document.getElementById(containerid).innerHTML=page_request.responseText;
}

function intDigits(e)
{
  var keynum;
  var keychar;
  var numcheck;
 
  if(window.event) // IE
  {
  	keynum = e.keyCode;
  }
  else if(e.which) // Netscape/Firefox/Opera
  {
  	keynum = e.which;
  }	

  	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;
	//return numcheck.test(keychar);
	if((keynum>=48 && keynum<=57 )|| keynum<=13)
	{
		return true;	
	}
	else
	{
		keynum=0;
		return false;
	}
	
}

function phone_Digits(e)
{
  var keynum;
  var keychar;
  var numcheck;
 
  if(window.event) // IE
  {
  	keynum = e.keyCode;
  }
  else if(e.which) // Netscape/Firefox/Opera
  {
  	keynum = e.which;
  }	
  
	if((keynum>=48 && keynum<=57)  || keynum == 45) 
	{
		return true;	
	}
	else
	{	
		keynum=0;	
		return false;
	}
}


function floatDigits()
{
	if(event.keyCode == 46 || (event.keyCode>=48 && event.keyCode<=57)) {}
	else
	{	event.keyCode=0;	}
}
function RowHoverIn(Row)
{
	Row.className='tblrowMousehover';
}

function RowHoverOut(Row,rowclass)
{
	Row.className=rowclass;
}	

function IsNumber(Expression)
{
	Expression = Expression.toLowerCase();
	RefString = ".0123456789";

	if (Expression.length < 1) 
		return (false);
	var count =0;
	for (var i = 0; i < Expression.length; i++) 
	{
		var ch = Expression.substr(i, 1)
		var a = RefString.indexOf(ch, 0)
		if (a == -1)
			return (false);
		if(ch=='.')	
			count++;
		if(count>1)	
			return false;
	}
	return(true);
}

function IsPhoneNumber(Expression)
{
	Expression = Expression.toLowerCase();
	RefString = "0123456789";

	//if (Expression.length < 1) 
		//return (false);
	var count =0;
	for (var i = 0; i < Expression.length; i++) 
	{
		var ch = Expression.substr(i, 1);
		var a = RefString.indexOf(ch, 0);

		if (a > -1)
			count++;			
	}
	if(count < 1)
		return false;
	else
		return(true);
}
function checkEmptyString(ctrlstring)
{	
	msg = "";
	msg1 = "";
	msgstring = "Please enter values for the following fields:\n";
	msgstring1 = "Please select atleast one value for the following fields:\n";
	ctrlfocus = "";

	ctrl = ctrlstring.split(";");

   for(i=0; i<ctrl.length; i++)
	{
		var ctrlname = ctrl[i].split(",");
		var a = ctrlname[0];
		if(a.substring(a.length-2,a.length) == "[]")
		{
			b = '"' + a + '.value' + '"';

          if(eval(b) == "")
			{
				if(ctrlfocus == "")
				{
					ctrlfocus = ctrlname[0] + ".focus()";
				}
				msg1 = msg1 + "\n" + ctrlname[1];
			}	
		}
		else
		{		
			if(eval(ctrlname[0]).value.split(" ").join("").length == 0)
			{
				if(ctrlfocus == "")
				{
					ctrlfocus = ctrlname[0] + ".focus()";
				}
				msg = msg + "\n" + ctrlname[1];
			}
		}
	}

if(msg.length > 0)
	{
		msgstring = msgstring + msg;
		return true;
	}
if(msg1.length > 0)
	{
		if(msg.length > 0)
			msgstring = msgstring + msgstring1 + msg1;
		else
			msgstring = msgstring1 + msg1;
	
	return true;
	}		
    return false;	
}

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_showHideLayers() 
{ //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}
function MM_openBrWindow()
{
	newwin =window.open('','PopupImage','width=150,height=150,resizable=1');
}

/*---------------------------------Image Zoom In /Zoom Out Function----------------------------------------*/

var ImageProductCode="";
var OriginalWidth=270;

function LargeImage()
{
	if(arguments.length<2)
	{
		ImageProductCode=arguments[0].replace("&","%26");
		//if(navigator.appName.toLowerCase().indexOf("microsoft")>-1)
		//{
			var html="<img id=\"productimages\" src=\"ProductImage.php?pc="+ImageProductCode+"&w="+OriginalWidth+"\">";
			document.getElementById("DivLargeImagePlaceHolder").innerHTML=html;
			document.getElementById("DivLargeImage").style.visibility="visible";
			document.onclick=HideImage;
		//}
		//else
		//{
		//	window.open("LargeImage.aspx?productcode="+arguments[0].replace("&","%26"), "largeimage", 'resizable=yes,menubar=no,status=no,location=no,scrollbars=no,top=0,left=0,width=600,height=600');
		//}
	}
	else
	{
		var strName=arguments[0];
		var intWidth=arguments[1];
		var intHeight=arguments[2];
		var strURL=arguments[3];
		objRefWindow = window.open(strURL, strName, 'resizable=yes,menubar=no,status=no,location=no,scrollbars=yes,width=' + intWidth + ',height=' + intHeight);
	}

}

function HideImage()
{
	document.getElementById("DivLargeImage").style.visibility="hidden";
}


function ImageZoomIn()
{
	var img=document.getElementById(arguments[0]);

	if(img.width>1000)
	{
		alert("Image zoom level reached its maximum limit");
		return;
	}

	w=img.width+270;

	var html="<img id=\"productimages\" src=\"ProductImage.php?pc="+ImageProductCode+"&w="+w+"\">";
	document.getElementById("DivLargeImage").style.visibility="visible";
	document.getElementById("DivLargeImagePlaceHolder").innerHTML=html;

}

function ImageZoomOut()
{

	var img=document.getElementById(arguments[0]);

	if(img.width<271)
		return;

	w=img.width-270;

	var html="<img id=\"productimages\" src=\"ProductImage.php?pc="+ImageProductCode+"&w="+w+"\">";
	document.getElementById("DivLargeImage").style.visibility="visible";
	document.getElementById("DivLargeImagePlaceHolder").innerHTML=html;
}


function RandomizeArray( myArray )
{
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i )
  {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
  }
  return myArray;
}



