// JavaScript Document
// JavaScript Document
//----------phone number validation-------------
	
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

	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++)
			{   
			// Check that current character isn't whitespace.
				var c = s.charAt(i);
				if (bag.indexOf(c) == -1) returnString += c;
				}
			return returnString;
			}
	function checkInternationalPhone(strPhone){
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	}

//----------end phone number validation-------------

function viewProduct()
{
	with (window.document.actionform) {
		if (cboCategory.selectedIndex == 0) {
			window.location.href = 'adm_Product.php';
		} else {
			window.location.href = 'adm_Product.php?catId=' + cboCategory.options[cboCategory.selectedIndex].value;
		}
	}
}

//select checkbox
function selectAll(field)
{
	for (i = 0; i < field.length; i++)
	{	
		if(document.getElementById("active").checked ==true)
		{
			field[i].checked = true ;
		}
		if(document.getElementById("active").checked ==false)
		{
			field[i].checked = false;
		}
	}
	
}
//select end

//submit javascript
function deleteAll(url)
{
	document.actionform.action=url;
	document.actionform.submit();
}
//end

//function to valid sites
function validSubject()
{
	var obj=document.subjectform;
	
	if (isEmpty(obj.subject, 'Please enter subject name!')) {
		return false;
	}if(obj.mark.value!=parseInt(obj.mark.value)){
		alert("Please enter your mark in integer format!");
		obj.mark.focus();
		obj.mark.select();
		return false;
	}

}
//end

//functoin valid request quote
function validquote()
{
	var obj=document.quoteform;
	if (isEmpty(obj.name, 'Please enter your name!')) {
		return false;
	}if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj.email.value)))
	{
		alert("Invalid E-mail Address! Please re-enter!")
		obj.email.focus();
		obj.email.select();
		return false;
	}if ((obj.phone.value==null)||(obj.phone.value=="")){
			alert("Please Enter your Phone Number!")
			obj.phone.focus();
			return false;
	}
	if (checkInternationalPhone(obj.phone.value)==false){
		alert("Please Enter a Valid Phone Number!")
		obj.phone.value="";
		obj.phone.focus();
		return false;
	}if (isEmpty(obj.residential, 'Please enter your Residential or Commercial!')) {
		return false;
	}if (isEmpty(obj.ser_ins, 'Please select Service or Installation!')) {
		return false;
	}
}
//end


//function valid testimonial
function contact_validation()
{
	var obj=document.contactForm;
	if (isEmpty(obj.name, 'Please enter your name.')) {
		return false;
	}if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj.email.value)))
	{
		alert("Invalid email address. Please re-enter.")
		obj.email.focus();
		obj.email.select();
		return false;
	}if ((obj.phone.value==null)||(obj.phone.value=="")){
			alert("Please enter your phone number.")
			obj.phone.focus();
			return false;
	}
	if (checkInternationalPhone(obj.phone.value)==false){
		alert("Please enter a valid phone number.")
		obj.phone.value="";
		obj.phone.focus();
		return false;
	}if (isEmpty(obj.comments, 'Please enter your message.')) {
		obj.comments.focus();
		return false;
	}
}
//end

//function valid comment
function validcomment()
{
	var obj=document.commentform;
	if (isEmpty(obj.name, 'Please enter your name!')) {
		return false;
	}if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj.email.value)))
	{
		alert("Invalid E-mail Address! Please re-enter!")
		obj.email.focus();
		obj.email.select();
		return false;
	}
}
//end


//function valid career form
function validc()
{
	var obj=document.careerform;
	if (isEmpty(obj.name, 'Please enter name!')) {
		return false;
	}if ((obj.phone.value==null)||(obj.phone.value=="")){
			alert("Please Enter your Phone Number!")
			obj.phone.focus();
			return false;
	}if (checkInternationalPhone(obj.phone.value)==false){
		alert("Please enter a valid Phone Number!")
		obj.phone.value="";
		obj.phone.focus();
		return false;
	}if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj.email.value)))
	{
		alert("Invalid E-mail Address! Please re-enter!")
		obj.email.focus();
		obj.email.select();
		return false;
	}
}
//end

//function unsubcription valid
function validunsub()
{
	var obj=document.subscribe_form;
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj.email.value)))
	{
		alert("Invalid E-mail Address! Please re-enter!")
		obj.email.focus();
		obj.email.select();
		return false;
	}if (isEmpty(obj.security_code, 'Please enter verfication code!')) {
		return false;
	}
}
//end

//valid contact form
function validContact()
{
	var obj=document.contactForm;
	if (isEmpty(obj.name, 'Please enter your Name!')) {
		return false;
	}if (isEmpty(obj.email, 'Please enter your Email!')) {
		return false;
	}if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj.email.value)))
	{
		alert("Invalid Email Address! Please re-enter!")
		obj.email.value='';
		obj.email.focus();
		obj.email.select();
		return false;
	}if ((obj.phone.value==null)||(obj.phone.value=="")){
			alert("Please Enter your Phone Number!")
			obj.phone.focus();
			return false;
	}if (checkInternationalPhone(obj.phone.value)==false){
		alert("Please enter a valid Phone Number!")
		obj.phone.value="";
		obj.phone.focus();
		return false;
	}if (isEmpty(obj.address, 'Please enter your address!')) {
		return false;
	}if (isEmpty(obj.state, 'Please enter your state!')) {
		return false;
	}if (isEmpty(obj.city, 'Please enter your city!')) {
		return false;
	}if (isEmpty(obj.zip, 'Please enter your zip code!')) {
		return false;
	}if (isEmpty(obj.comments, 'Please enter your comments!')) {
		return false;
	}
}
//end



/*password generate function*/
function GeneratePassword() {
    if (parseInt(navigator.appVersion) <= 3) {
        alert("Sorry this only works in 4.0+ browsers");
        return true;
    }

    var length=8;
    var sPassword = "";
    length = document.userform.charLen.options[document.userform.charLen.selectedIndex].value;

    var noPunction = (document.userform.punc.checked);
    var randomLength = (document.userform.rLen.checked);

    if (randomLength) {
        length = Math.random();

        length = parseInt(length * 100);
        length = (length % 7) + 6
    }


    for (i=0; i < length; i++) {

        numI = getRandomNum();
        if (noPunction) { while (checkPunc(numI)) { numI = getRandomNum(); } }

        sPassword = sPassword + String.fromCharCode(numI);
    }
  	document.userform.password.value = sPassword

    return true;
}

function getRandomNum() {

    // between 0 - 1
    var rndNum = Math.random()

    // rndNum from 0 - 1000
    rndNum = parseInt(rndNum * 1000);

    // rndNum from 33 - 127
    rndNum = (rndNum % 94) + 33;

    return rndNum;
}

function checkPunc(num) {

    if ((num >=33) && (num <=47)) { return true; }
    if ((num >=58) && (num <=64)) { return true; }
    if ((num >=91) && (num <=96)) { return true; }
    if ((num >=123) && (num <=126)) { return true; }

    return false;
}

/*end password generate function*/





/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

//check integory value only
function checkInt(textBox)
{
	if(textBox.value!=parseInt(textBox.value)){
		textBox.value =	"";
	}
	/*while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value =	textBox.value.replace(/./,"W");
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}*/
	textBox.value = trim(textBox.value);
}
//end

//check integory value only
function checkMark(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value =	textBox.value.replace(/./,"W");
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	textBox.value = trim(textBox.value);
}
//end


/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}
/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}


/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}
function checkPhoto(picField) {
	var fup = document.getElementById(picField);
	var fileName = fup.value;
	var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
	
	if(ext == "gif" || ext == "GIF" || ext == "JPEG" || ext == "jpeg" || ext == "jpg" || ext == "JPG" || ext == "png" || ext == "PNG" || ext == "BMP" || ext == "bmp")
	
	{
	
	return true;
	
	} 
	
	else
	
	{
	
		alert("Upload jpg,gif,jpeg,png type images only");
		fup.focus();
		document.getElementById(picField).value="";
		return false;
	
	}

}
//end

//function to valid events
function validEvents()
{
	var obj=document.eventform;
	if (isEmpty(obj.title, 'Please enter event title!')) {
		return false;
	}
}
//end

//generat popup 
function popupReport(url)
{
	window.open(url, "secpod", "status = 1, height = 300, width = 450, resizable = 0, screenX=500, screenY=200"); 
}
//end
//window property
function windowredirect(page)
{
	location.href=page;
}
//end

//add more upload
function openAttachment()
{
	var  val=parseInt(document.getElementById("hfile").value);
	document.getElementById("hid"+val).style.display="block";
	document.getElementById("hfile").value=val+1;
}
//end
//remore upload
function removeAttachment(val)
{
	document.getElementById("hid"+val).style.display="none";
	/*var  val=parseInt(document.getElementById("hfile").value-1);
	document.getElementById("hid"+val).style.display="none";
	if(document.getElementById("hfile").value!=1){
	document.getElementById("hfile").value=val;
	}*/
	
}
//end
//function news letter preview
function showpreview()
{
	var subject=document.newsForm.subject.value;	
	var message=document.newsForm.message.value;
	window.open("preview.php?subject="+subject+"&message="+message, "secpod", "width=800,height=600,scrollbars=yes");
}
//end

//function to generate code
function generate()
{
	var obj=document.codeform;
	alert(obj);
	var language=obj.language.value;
	var code=obj.code.value;
	alert(language);
	alert(code);
}
//end
