// 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



//valid contact form
function validContact()
{
	var obj=document.contactForm;
	if (isEmpty(obj.name, 'Please enter your name!')) {
		return false;
	}if (obj.name.value=="Name:"){
		alert("Please enter your name!");
		obj.name.focus();
		obj.name.value='';
		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

/* validate teall a friend form*/
function validTellFriend()
{
	var obj=document.tellfriendForm;
	if (isEmpty(obj.your_email, 'Pleaser enter your email!')) {
		return false;
	}if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj.your_email.value))){
			alert("Invalid E-mail Address! Please re-enter!")
			obj.your_email.focus();
			obj.your_email.select();
			return false;
	}if (isEmpty(obj.friends_email, 'Pleaser enter your email!')) {
		return false;
	}if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obj.friends_email.value))){
			alert("Invalid E-mail Address! Please re-enter!")
			obj.friends_email.focus();
			obj.friends_email.select();
			return false;
	}if (isEmpty(obj.message, 'Pleaser enter your message!')) {
		return false;
	}else
	{
		obj.action='tellafriend.php?action=tellafriend';
		obj.submit();
	}

	
}
/* end tell a friend form */


//function remove text
function removetext(text,field)
{
	var obj=document.contactForm;
	
	if(field=='name' && text=='Name:')
	{
		obj.name.value='';
	}
	if(field=='phone' && text=='Phone:')
	{
		obj.phone.value='';
	}
	if(field=='email' && text=='Email:')
	{
		obj.email.value='';
	}
	if(field=='comments' && text=='Message:')
	{
		obj.comments.value='';
	}
}
//end

//fill blank
function redue(text,field)
{
	var obj=document.contactForm;
	if(field=='name' && text==''){
		obj.name.value='Name:';
	}if(field=='phone' && text==''){
		obj.phone.value='Phone:';
	}if(field=='email' && text==''){
		obj.email.value='Email:';
	}if(field=='comments' && text==''){
		obj.comments.value='Message:';
	}

}
//end

//valid online tenancy form
function validtenancy()
{
	var obj=document.frmSelect;
	
	if (isEmpty(obj.tenantname, 'Please enter tenant!')) {
		return false;
	}if (isEmpty(obj.room, 'Please select room!')) {
		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((obj.tntnxtkin.value==null)||(obj.tntnxtkin.value=="")){
			alert("Please Enter your Tel of Next of Kin!")
			obj.tntnxtkin.focus();
			return false;
	}
	if (checkInternationalPhone(obj.tntnxtkin.value)==false){
		alert("Please Enter a Valid Tel of Next of Kin!")
		obj.tntnxtkin.value="";
		obj.tntnxtkin.focus();
		return false;
	}if(obj.tnc.checked==false){
		alert("Please check the terms and conditions checkbox!")	
		return false;
	}
}
//end

//function for valid site emails
function validSiteEmail()
{
	var obj=document.emailForm;
	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.parent, 'Please enter site name!')) {
		return false;
	}
}
//end

function validArticle()
{
	var obj=document.newsFrom;
	if (isEmpty(obj.date, 'Please enter date!')) {
		return false;
	}if (isEmpty(obj.heading, 'Please enter year!')) {
		return false;
	}
}

function validMenu()
{
	var obj=document.menuform;
	if (isEmpty(obj.name, 'Please enter name!')) {
		return false;
	}
}

function validcontent()
{
	var obj=document.contentform;
	if (obj.parent.value==0) {
		alert("Please select menu!");
		obj.parent.focus();
		return false;
	}if (isEmpty(obj.title, 'Please enter title!')) {
		return false;
	}
}


//valid student details
function validForm()
{
	var obj=document.regform;
	if (isEmpty(obj.name, 'Please enter full 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.user_id, 'Please enter user name!')) {
		return false;
	}if (isEmpty(obj.password, 'Please enter your password!')) {
		return false;
	}if(obj.password.value.length<6){
		alert("Please enter 6 or more than 6 characters!");
		obj.password.focus();
		return false;
	}
}
//end

function validComments()
{
	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;
	}if (isEmpty(obj.message, 'Please enter your message!')) {
		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
