﻿var ObjectID = ""
var ObjectTitle = ""

function openObject(){
   var odiv = event.srcElement;
   if (odiv.ObjectID == "") return;
   openObjectWindow(odiv.ObjectID,odiv.ObjectType)
}

function product_clicked(){
	var o = event.srcElement;
	if (o.tagName == "AREA")
	{
		var path = "/product.aspx?pid=" + o.alt;
		document.location.href = path;
	}
}

function checkform(obj) {
		
		var a = 0;
		var b = 0;
		var i = '';
	
		if (obj.name.value == "")    { i = i + "please enter name.\n"; var a = 1; };
		if (obj.phone.value == "")   { i = i + "please enter phone number.\n"; var a = 1; };
		if (obj.company.value == "") { i = i + "please enter .\n"; var a = 1; };
		
		if (obj.address.value == "") { i = i + "please enter address.\n"; var a = 1; };
		if (obj.state.value == "")   { i = i + "please choose state.\n"; var a = 1; };
		if (obj.country.value == "") { i = i + "please choose country.\n"; var a = 1; };
		
		if (obj.type.value == "")    { i = i + "please choose type.\n"; var a = 1; };
		if (obj.email.value =="")    { i = i + "please enter E-mail address.\n"; var a = 1; };
				
		if (a != 0) { 
			alert(i); 
			return false; 
		}
		else
			return true;
		
		}
		
		
		function checkQty (obj) {

			var qty = Math.round(obj.qty.value);
			
			if (isNaN(obj.qty.value) == true) 
				alert("The value must be a number");
				else
				{
					if (obj.qty.value < 1)
					alert("The value must be a positive number");
					else
					{
						if (qty == obj.qty.value)
							obj.submit();
						else
							alert("The value can't be a fraction");
					}
				}
		}
		
	
		
		function checkXslQty (obj) {
	
		var qty = Math.round(obj.qty.value);
			
			if (isNaN(obj.qty.value) == true) 
			{
				alert("The value must be a number");
				return false;
			}
			else
			{
				if (obj.qty.value < 1)
				{
					alert("The value must be a positive number");
					return false;
				}
				else
				{
					if (qty == obj.qty.value)
						return true;
					else
					{
						alert("The value can't be a fraction");
						return false;
					}
				}
			}
							
		//debugger
			/*if (isNaN(obj.qty.value) == true) { 
				alert("The value must be a number");
				return false;
				}
				else
				return true;*/
		}
		
function objParams (oDiv){
   this.SerchString = oDiv.pname
   this.ProductID 
   this.ProductName
}	

function searchBar()
{
	var url = "/common/webcontrols/productsearch/gridsearch.aspx?"
	url += "searchstring=" + frmSearch.searchtext.value;
	//searchtext.disabled = true;
	frmSearch.searchtext.focus();
	frmGridSearch.location.href = url;
}

function searchBarFalse()
{
	var url = "/common/webcontrols/productsearch/gridsearch.aspx?"
	url += "searchstring=" + frmSearch.searchtext.value;
	//searchtext.disabled = true;
	frmSearch.searchtext.focus();
	frmGridSearch.location.href = url;
	return false ;
}		
function openProductSearch(){

   var oimg = event.srcElement;
   var odiv = oimg.parentElement.previousSibling.firstChild;
   var sUrl = "/common/webcontrols/productsearch/productsearch.aspx?"
   sUrl += "searchstring=" + odiv.value;
   
   var w = 400;
   var h = 450;
   var Params = new objParams(odiv)
	var oResult = window.showModalDialog(sUrl, Params, "dialogWidth:" + w + "px;dialogHeight:" + h + "px;help:0;status:0;scroll:0;center:1");
  
   if (oResult){
     //oimg.parentElement.nextSibling.firstChild.value = oResult.ProductID
     odiv.value = oResult.ProductName
   }
}

var LastTD ; 
var LastParentTR;




//Modified by Tal Tzur on 28/7/2005
function RowClicked()
{
 var obj = event.srcElement;
    switch (obj.tagName)
	{
		case "NOBR": obj = obj.parentElement; break;
		case "IMG": obj = obj.parentElement; break;
		default:     
	}
	var parentTR = obj;
	while (parentTR.tagName != "TR")
	{
	   parentTR = parentTR.parentElement;
	   if (parentTR == null) return; 	
	}
	if (!parentTR.ObjectID) {return false;}
	
	
	//var page_name=document.getElementById("pagename").text
	var page_name=document.getElementById("pagename").defaultValue
	switch(page_name)
	{
		case "myorders" :
	 		var url = "http://crm.yvel.co.il/_common/reports/webreports/r1003.aspx?objectid=" + parentTR.ObjectID + "&AccountView=1"
			break;
		case "myinvoices" :
		
		    var url = "http://crm.yvel.co.il/_common/Reports/webreports/R1060.aspx?objectID=" + parentTR.ObjectID + "&printType=0"
		              
			
		break;
		case "mycreditinvoices" :
		
		break;

	}
	
	
	
//	var url = "http://crm.yvel.co.il/_common/reports/webreports/r1003.aspx?objectid=" + parentTR.ObjectID + "&AccountView=1"
	window.open(url)
}



function printStatement(accountid)
{
var page_name=document.getElementById("pagename").text
	switch(page_name)
	{
	case "myorders" :
 		var url = "http://crm.yvel.co.il/_common/reports/webreports/r1007.aspx?objectid=" + accountid;
 		break;
	case "myinvoices" :
  		var url = "http://crm.yvel.co.il/_common/reports/webreports/r1060.aspx?objectid=" + accountid;
  		break;
	case "mycreditinvoices" :
		var url = "http://crm.yvel.co.il/_common/reports/webreports/r1059.aspx?objectid=" + accountid;
		break;
	}	
	
	window.open(url);
}

function ChooseRow()
{
 var obj = event.srcElement;
    switch (obj.tagName)
	{
		case "NOBR": obj = obj.parentElement; break;
		case "IMG": obj = obj.parentElement; break;
		default:     
	}
	var parentTR = obj;
	while (parentTR.tagName != "TR")
	{
	   parentTR = parentTR.parentElement;
	   if (parentTR == null) return; 	
	}
	if (!parentTR.ObjectID) {return false;}
	window.parent.setData(parentTR);
	//if(LastTD){
	//  LastTD.runtimeStyle.backgroundColor = "#ffffff"
	//}
	//obj.runtimeStyle.backgroundColor = "#ADC3E7";
	//LastTD = obj;
	if(LastParentTR){
	   LastParentTR.runtimeStyle.backgroundColor = "#ffffff"
	}
	parentTR.runtimeStyle.backgroundColor = "#ADC3E7";
	LastParentTR = parentTR;
}

function setData(oTR){
   try{
     ObjectID = oTR.ObjectID ; 
     ObjectTitle = oTR.firstChild.innerText ; 
   }
   catch(e){};
}

function Result () {
   this.ProductID = ObjectID
	this.ProductName = ObjectTitle
}

function save(){
  if (ObjectID != "" && ObjectTitle != ""){
     var result = new Result() ;
     window.returnValue = result ;
     CloseWindow();
  }
}

function mouseOver(obj) {
  //   obj.runtimeStyle.backgroundColor = "#F7F3E7";
    $(obj).css('backgroundColor', '#F7F3E7')
}

function mouseOut(obj) {
   //  obj.runtimeStyle.backgroundColor = "#ffffff";
     $(obj).css('backgroundColor', '#ffffff')
}

function CloseWindow(){ window.close();}

function confirmOrder()
{
	document.getElementById("desc").submit();
}


function RowClicked2(param)
{
    var obj = event.srcElement;
    switch (obj.tagName)
	{
		case "NOBR": obj = obj.parentElement; break;
		case "IMG": obj = obj.parentElement; break;
		default:     
	}
	var parentTR = obj;
	while (parentTR.tagName != "TR")
	{
	   parentTR = parentTR.parentElement;
	   if (parentTR == null) return;
    }
if (!$(parentTR).attr("objectID")) { return false; }
 //   top.window.location.href = "/printdoc.aspx?oid=" + parentTR.ObjectID + "&doctype=" + param
    top.window.location.href = "/printdoc.aspx?oid=" + $(parentTR).attr("objectID") + "&doctype=" + param
	
}

function RowClicked3(param) //stores + contacts
{
 var obj = event.srcElement;
    switch (obj.tagName)
	{
		case "NOBR": obj = obj.parentElement; break;
		case "IMG": obj = obj.parentElement; break;
		default:     
	}
	var parentTR = obj;
	while (parentTR.tagName != "TR")
	{
	   parentTR = parentTR.parentElement;
	   if (parentTR == null) return; 	
	}
if (!$(parentTR).attr("objectID")) { return false; }
	if (param == "1") // Stores
	    top.window.location.href = "/editStore.aspx?oid=" + $(parentTR).attr("objectID") // + "&doctype=" + param
            else if (param == "2") // Contacts
                top.window.location.href = "/editContact.aspx?oid=" + $(parentTR).attr("objectID") // + "&doctype=" + param
        
}

function printdownframe(printframe)
{
    document.printframe.focus(); 
    window.print(); 
    
    //window.frames['printframe'].focus(); 
    //window.frames['printframe'].print(); 
    //printframe.focus();
    //printframe.print();

}



// Declaring valid date character, minimum year and maximum year******/*
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function ValidateForm(){
	var dt=document.all.addate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }

