var ShowRoomFlag;
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 dtCh= "-";
	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 ){
		alert("Please enter a valid 4 digit year");
		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 isEmpty(obj) {
	var x = obj.value;
	if(x == "" || x == null || x == 0) {
		obj.focus();
    var field = obj.name.charAt(0).toUpperCase()+obj.name.substring(1, obj.name.length);
	
		alert("Required field should not be empty");
		return true;
	}
	for(i = 0; i < x.length; i++) {
		if(x.charAt(i) != " ")
		return false;
	}
	obj.focus();
	return true;
}
function validateEmailID(obj) {
  var val = obj.value;
  var leng = val.length;
  var dotPosition = val.lastIndexOf(".");
  if(val == "" || val.indexOf(".")== -1 
	|| val.indexOf("@") != val.lastIndexOf("@") 
	|| leng-dotPosition>=6
	|| val.indexOf("@")==-1 ) {
    alert("Emailid should be a valid one");
    obj.focus();
    return false;	
  }
    return true;
}	

function validateNumber(obj) {
  var field = obj.name.toUpperCase();
	if(isNaN(obj.value)) {
		alert(field + " should be numbers only");
		obj.focus();
		return false;
	 } else if(parseInt(obj.value, 10) <= 0) {
		alert(field + " should be greater than 0");
		obj.focus();
		return false;	
	 }	
	 return true;
}	 

function isEmptyString(fieldValue) { 
	if(fieldValue == "" || fieldValue == null || fieldValue == 0) {
		return true;
	}
	for(i = 0; i < fieldValue.length; i++) {
		if(fieldValue.charAt(i) != " ")
		return false;
	}
	return true;
}

function isLength(obj) {
	var x = obj.value;
	if(x.length <6) {
		obj.focus();
    var field = obj.name.toUpperCase();
		alert(field + " minimum six characters");
		return true;
	}
	for(i = 0; i < x.length; i++) {
		if(x.charAt(i) != " ")
		return false;
	}
	obj.focus();
	return true;
}


function login() {
	var userName = document.login_form.username.value;
	var password = document.login_form.password.value;
	if(isEmptyString(userName)) {
		alert("User name should not be empty.");
		document.login_form.username.focus();
		return;
	} else if(isEmptyString(password)) {
		alert("Password should not be empty.");
		document.login_form.password.focus();
		return;
	}
      
        document.login_form.username.value = trimFunction(document.login_form.username.value);
	document.login_form.password.value = trimFunction(document.login_form.password.value);	
	document.login_form.submit();
      
        
        
 }


// Scripts for OrderHistory start

function viewOrder(orderId) {
	var win=window.open('/hindu/orderSummary.do?orderId=' + orderId,'Start','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=450');
}

function download(orderId) {
	document.orderHistory.orderId.value = orderId;
	document.orderHistory.action = "/hindu/downloadPicture.do";
	document.orderHistory.submit();
}

// Scripts for OrderHistory end

// Scripts for AccountHistoryDetail start

function loadAccountDetails(combo) {
	var value = combo.options[combo.selectedIndex].value;	
	if(value == "G") {
		document.orderHistory.action = "/hindu/giftCertificateDetail.do";
		document.orderHistory.ACTION.value = "giftDetail";
		document.orderHistory.length.value = 5;
                document.orderHistory.offset.value = 0;
                document.orderHistory.page.value = 0;
                document.orderHistory.size.value = 0;
                document.orderHistory.pageSize.value = 0;
                document.orderHistory.submit();
	} else if(value == "P") {
		document.giftCertificateDetail.action = "/hindu/orderHistory.do";		
		document.giftCertificateDetail.length.value = 5;
                document.giftCertificateDetail.offset.value = 0;
                document.giftCertificateDetail.page.value = 0;
                document.giftCertificateDetail.size.value = 0;
                document.giftCertificateDetail.pageSize.value = 0;
		document.giftCertificateDetail.submit();
	}
}

// Scripts for AccountHistoryDetail end


function loadCollection(combo) {
	document.logon.action = "/hindu/advancedSearch.do";
	document.logon.collectionName.value = combo.options[combo.selectedIndex].text;
	document.logon.submit();
}

function quickPurchase() {
	document.logon.action = "/hindu/orderPayment.do";
	document.logon.submit();
}

function trimFunction(trimValue) {	
	return trimValue.replace(/^\s+/, '').replace(/\s+$/, '');
}

function printableView(url) {
	window.open(url,'newWin','menubar=yes,location=no,scrollbars=yes,resizable=yes,width=800,height=600,left=0,top=0');
}


function checkDates(from, to) {
	 var fromDate = from.value;
	 var toDate = to.value; 		

	 if( fromDate != ""){
		if(!isDate(fromDate)) {
			from.focus();
			return false;
		}
	} else if(fromDate == "" &&  toDate != "") {
		alert("Enter value in From");	
		from.focus();
		return false;	
	}
	 if(toDate != "") {
		if(!isDate(toDate)) {
			to.focus();
			return false;
		}
	} 

	if(new Date(fromDate.substring(3,5) + "/" + fromDate.substring(0,2) + "/" 
		+ fromDate.substring(8,10)) >  new Date(toDate.substring(3,5) + "/" +
		 toDate.substring(0,2) + "/" + toDate.substring(8,10))) {
		alert("To date should be greater than from date");	
		to.focus();
		return false;
	}
	return true;
}

function showShowRoom()
{
	//alert("ShowRoomFlag " + ShowRoomFlag);
	/*if(ShowRoomFlag==true){
		document.getElementById("showroom_country").style.visibility = "visible";
		document.getElementById("showroom_location").style.visibility = "visible";
	}*/
	var arryTags = document.getElementsByTagName("select");
	for(i=0;i<arryTags.length;i++) {
		arryTags[i].style.visibility = "visible";
		arryTags[i].style.visibility = "visible";
	}

}

function HideShowRoom()
{
	//alert("ShowRoomFlag " + ShowRoomFlag);
	/*if(ShowRoomFlag==true){
		document.getElementById("showroom_country").style.visibility = "hidden";
		document.getElementById("showroom_location").style.visibility = "hidden";
	}*/
	var arryTags = document.getElementsByTagName("select");
	for(i=0;i<arryTags.length;i++) {
		arryTags[i].style.visibility = "hidden";
		arryTags[i].style.visibility = "hidden";
	}
}

function setShowRoomFlag()
{
	//alert(" inside setshowroomflag");
	ShowRoomFlag = true;
	//alert("ShowRoomFlag " + ShowRoomFlag);
}

function resetShowRoomFlag()
{
	ShowRoomFlag = false;
}
